Upgrade to Pro — share decks privately, control downloads, hide ads and more …

.NET Day 2024: Mastering Integration Testing fo...

dotnetday
September 11, 2024

.NET Day 2024: Mastering Integration Testing for .Net Web Apis With Webapplicationfactory and Testcontainers

More info at: https://dotnetday.ch

dotnetday

September 11, 2024
Tweet

More Decks by dotnetday

Other Decks in Technology

Transcript

  1. ABOUT ME M A R C R U F E

    R S O F T WA R E E N G I N E E R I S O LU T I O N S A G R U F E R 7 M A R C R U F E R A black and white sign with white text Description automatically generated
  2. AGENDA I N T R O D U C T

    I O N I N T E G R AT I O N T E S T I N G I N . N E T A B O U T D ATA B A S E S A N D T E S T I N G T E S TC O N TA I N E RS C I / C D F I N A L T I P S
  3. WHAT “Testing performed to expose defects in the interfaces and

    in the interactions between integrated components or systems” ISTQB Glossary
  4. WHAT “Integration tests ensure that an app's components function correctly

    at a level that includes the app's supporting infrastructure, such as the database, file system, and network” Microsoft Learn
  5. WHY ▪ Fail fast & fail early ▪ Can include

    middleware, configuration, DI, … ▪ REST endpoints, request-response pipeline and serialization / deserialization can be tested ▪ Executable in CI pipeline (automation)
  6. HOW ▪ Create separate integration test project ▪ Reference the

    System under test (SUT) ▪ Create test web host for the SUT ▪ Use test server client to handle requests and responses ▪ Use test runner to execute and report
  7. INTEGRATION TESTS SAMPLE CODE • B U I LT O

    N TO P O F B L A Z O R . B F F. A Z U R E A D.T E M P L AT E ( M I C R O S O F T E N T R A I D ) • S Q L D ATA B A S E D E P E N D E N C Y & E F C O R E A D D E D
  8. IN-MEMORY DATABASES LocalDB Only available on Windows SQLite in-memory DB

    SQLite EF Core Database Provider does especially not natively support the following types • DateTimeOffset • Decimal • TimeSpan • UInt64
  9. CI/CD ▪ Docker required ▪ GitHub GitHub-hosted agents -> Docker

    is preinstalled ▪ Azure DevOps Microsoft-hosted agents -> Docker is preinstalled
  10. TIPS & NOTES ▪ Don’t forget to run Docker Desktop

    locally ▪ Windows agents cannot run all containers ▪ Fake AntiForgery token validation instead of deregistering it ▪ Fully debuggable ▪ Assert REST API responses with Verify.Xunit
  11. USEFUL LINKS ▪ Integration tests in ASP.NET Core (Microsoft docs)

    ▪ Testcontainers ▪ GitHub repository with sample code ▪ [HOWTO] Implement integration tests for ASP.NET Core Web API with AntiForgery token validation ▪ [FollowUp] Using Testcontainers in integration tests for ASP.NET Core Web API ▪ [NoBrainer] Avoid HTTPS redirection warnings in integration test logs