MVCApp1.UnitTestsprovides unit tests for controllers and integration testsMVCApp1.PlaywrightTestsprovides headless smoke coverage using Playwright against a running instance of the site- Controllers have a coverage target of ≥90%
-
Create a test project (e.g.,
MVCApp1.Tests):dotnet new xunit -n MVCApp1.Tests dotnet add MVCApp1.Tests/MVCApp1.Tests.csproj reference MVCApp1/MVCApp1.csproj
-
Add tests for controllers and services.
-
Update the solution file to include the test project:
dotnet sln add MVCApp1.Tests/MVCApp1.Tests.csproj
- Locally:
dotnet test - Playwright browsers (first run):
dotnet build tests/MVCApp1.PlaywrightTestsfollowed bypwsh bin/Debug/net9.0/playwright.ps1 installor simply execute the tests once and the suite will install browsers automatically. - CI (GitHub Actions): Automatically installs Playwright via
Microsoft.Playwright.CLIand runsdotnet testwhen test projects exist.
- Controller tests with mocked dependencies
- Service tests (when services are added)
- Integration tests for application startup
- Smoke tests for critical user flows
- Browser-based testing with Playwright
- Tests against actual running application
- Use AAA (Arrange-Act-Assert) pattern.
- Mock dependencies (e.g.,
ILogger<T>, services) where appropriate. - Keep tests deterministic and isolated.
- Use
WebApplicationFactory<Program>for integration tests. - Test both success and error paths.
- Name tests descriptively:
MethodName_Scenario_ExpectedBehavior.