Testing Patterns
Test strategies for full-stack applications.
Test Pyramid
code
/\
/E2E\ Few, slow, critical paths
/------\
/ Integ- \ Some, key integrations
/--ration--\
/ Unit \ Many, fast, isolated
/--------------\
| Type | Speed | Scope |
|---|---|---|
| Unit | Fast | Single function/class |
| Integration | Medium | Multiple components |
| E2E | Slow | Full user flow |
AAA Pattern
csharp
[Fact]
public void Method_Scenario_Expected()
{
// Arrange - Set up test data
var input = CreateInput();
// Act - Execute the code
var result = Process(input);
// Assert - Verify outcome
Assert.Equal(expected, result);
}
Test Naming
Format: Method_Scenario_ExpectedResult
| ❌ Bad | ✅ Good |
|---|---|
| TestAdd | Add_TwoPositive_ReturnsSum |
| Test1 | GetUser_InvalidId_ThrowsException |
What to Test
| ✅ Test | ❌ Skip |
|---|---|
| Business logic | Framework code |
| Edge cases | Getters/setters |
| Error handling | Third-party libs |
| Public API | Implementation details |
Mocking Guidelines
| Mock | Don't Mock |
|---|---|
| External APIs | The code under test |
| Database | Simple objects |
| File system | Pure functions |
| Time/Random | Business logic |
Code Coverage
| Metric | Target |
|---|---|
| Overall | 70-80% |
| Business logic | 90%+ |
| Controllers | 60%+ |
| Infrastructure | Lower priority |
DO / DON'T
| ✅ Do | ❌ Don't |
|---|---|
| One concept per test | Test multiple things |
| Descriptive names | Cryptic test names |
| Independent tests | Tests that depend on order |
| Test edge cases | Only happy path |