MASTERING INTEGRATION
TESTING FOR .NET WEB APIS
WITH
WEBAPPLICATIONFACTORY AND
TESTCONTAINERS
Slide 2
Slide 2 text
SPONSORS OF
.NET DAY SWITZERLAND 2024
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
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
Slide 5
Slide 5 text
INTEGRATION TESTING IN .NET
Slide 6
Slide 6 text
WHAT
“Testing performed to expose defects in the
interfaces and in the interactions between
integrated components or systems”
ISTQB Glossary
Slide 7
Slide 7 text
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
Slide 8
Slide 8 text
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)
Slide 9
Slide 9 text
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
Slide 10
Slide 10 text
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
Slide 11
Slide 11 text
REALITY VS. INTEGRATION TESTING
Slide 12
Slide 12 text
DEMO TIME
INTEGRATION TESTS IN ASP.NET CORE
Slide 13
Slide 13 text
ABOUT DATABASES AND
TESTING
Slide 14
Slide 14 text
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
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
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