This skill should be used when you need to "write tests", "create test cases", "set up test fixtures", "run test suite", "improve coverage", or "implement TDD".
Unit, integration, and E2E testing patterns for comprehensive quality assurance.
| Layer | Tool | Purpose |
|---|---|---|
| Unit (Python) | pytest | Function/method testing |
| Unit (JS/TS) | Jest / Vitest | Component/function testing |
| Integration | pytest / supertest | API endpoint testing |
| E2E | Playwright | Full user flow testing |
| Coverage | coverage.py / c8 | Coverage reporting |
tests/
├── unit/ # Isolated unit tests
│ ├── test_models.py
│ ├── test_services.py
│ └── test_utils.py
├── integration/ # API/service integration
│ ├── test_endpoints.py
│ └── test_repositories.py
└── e2e/ # Full user flows
├── pages/ # Page objects
└── flows/ # Test scenarios
1. RED: Write a failing test
2. VERIFY: Confirm it fails for the right reason
3. GREEN: Write minimal code to pass
4. VERIFY: Confirm ALL tests pass
5. REFACTOR: Improve code quality (keep tests green)
6. REPEAT: Next test case
@pytest.fixture
def db_session():
"""Provide a clean database session for each test."""
session = TestingSessionLocal()
yield session
session.rollback()
session.close()
@pytest.fixture
def sample_data(db_session):
"""Create sample data for testing."""
item = Model(name="test", value=42)
db_session.add(item)
db_session.commit()
return item
beforeEach(() => {
// Reset mocks and state
jest.clearAllMocks();
});
const createMockData = () => ({
id: 1,
name: 'test',
value: 42,
});
def test_calculate_total_with_discount():
"""Test that discount is applied correctly."""
# Arrange
items = [Item(price=100), Item(price=200)]
discount = 0.1
# Act
total = calculate_total(items, discount)
# Assert
assert total == 270 # (100 + 200) * 0.9
def test_create_item_endpoint(client, db_session):
"""Test creating an item via API."""
response = client.post("/api/items", json={
"name": "Test Item",
"value": 42,
})
assert response.status_code == 201
assert response.json()["name"] == "Test Item"
test('user can create an item', async ({ page }) => {
await page.goto('/items/new');
await page.fill('[name="itemName"]', 'Test Item');
await page.click('button[type="submit"]');
await expect(page.locator('.success-message')).toBeVisible();
});
| Scope | Target |
|---|---|
| Overall | 80%+ |
| Critical paths (auth, data) | 95%+ |
| New code | 90%+ |
| Utils/helpers | 100% |
# Python
pytest tests/ # All tests
pytest tests/unit/ # Unit only
pytest -x # Stop on first failure
pytest --cov=app --cov-report=html # With coverage
# JavaScript/TypeScript
npm test # All tests
npm test -- --watch # Watch mode
npm test -- --coverage # With coverage
test_create_item_with_invalid_name_returns_400