Testing কেন?
- Bug early catch — cost 100x কম
- Refactor confidently
- Documentation হিসেবে কাজ করে
- Regression prevent
Test Pyramid ⭐
/\
/E2E\ Few (slow, expensive)
/------\
/Integra-\ Some
/ tion \
/------------\
/ Unit Tests \ Many (fast, cheap)
/--------------\
Types
| Type | Scope |
|---|---|
| Unit | 1 function/class (mock deps) |
| Integration | Multiple components (real DB) |
| Contract | API shape (consumer/provider) |
| E2E | Full user flow (browser) |
| Smoke | Critical paths post-deploy |
| Regression | Old bugs don't return |
| Acceptance (UAT) | Business validates |
Unit Test — AAA Pattern
[Fact]
public void Add_Two_Numbers() {
// Arrange
var calc = new Calculator();
// Act
var result = calc.Add(2, 3);
// Assert
Assert.Equal(5, result);
}
Test Naming
MethodUnderTest_Scenario_ExpectedBehavior Withdraw_InsufficientBalance_ThrowsException Login_ValidCredentials_ReturnsToken CalculateDiscount_VIPCustomer_Returns20Percent
Mocking
// Moq (.NET)
var repo = new Mock<IUserRepository>();
repo.Setup(r => r.GetById(1))
.ReturnsAsync(new User { Id = 1, Name = "Rahim" });
var service = new UserService(repo.Object);
var user = await service.GetUserAsync(1);
Assert.Equal("Rahim", user.Name);
repo.Verify(r => r.GetById(1), Times.Once);
TDD (Test-Driven Development) ⭐
Red — failing test লিখুন ↓ Green — pass করার minimum code ↓ Refactor — clean up (tests green রাখুন) ↓ repeat
BDD (Behavior-Driven) ⭐
Feature: User Login
Scenario: Valid credentials
Given a registered user with email "rahim@x.com"
When they login with correct password
Then they should see the dashboard
And receive a JWT token
Tools: Cucumber, SpecFlow (.NET), Behat (PHP)।
Code Coverage
- Line coverage — which lines run
- Branch coverage — if/else both sides
- Target: 70-80% sensible
- ❌ 100% target counterproductive
- Tools: Jest, coverlet, JaCoCo, Istanbul
Test Doubles
- Dummy — placeholder, not used
- Stub — fake return value
- Spy — record calls
- Mock — verify behavior
- Fake — working simpler impl (in-memory DB)
Testing Frameworks
xUnit/NUnit (.NET), Jest/Vitest (JS), pytest (Python), JUnit (Java), PHPUnit (PHP), RSpec (Ruby)।
Best Practices
- Fast (< 1s unit)
- Isolated (no shared state)
- Repeatable (same input = same output)
- Self-validating (pass/fail, no manual check)
- Timely (write with code, not after)
- One assertion concept per test
- Test behavior, not implementation