Writing Tests
Rules
- •Reuse test files - Add tests to existing files when appropriate
- •Use parametrized tests - Group cases with same expected behavior via
@pytest.mark.parametrize - •No obvious comments - Code should be self-explanatory
- •Follow existing style - Match patterns in the existing test suite
- •Imports at module level - Only import inside functions when absolutely necessary
- •Reuse fixtures - Use existing fixtures; create new ones only when needed
Test Naming
Name tests using the pattern: test_<what>_<condition>_<outcome>
Examples:
- •
test_login_valid_credentials_succeeds - •
test_process_payment_insufficient_funds_raises_error - •
test_parse_json_invalid_input_returns_none
Test Structure
- •Test all paths - Happy path, edge cases, and error conditions
- •One assertion focus - Each test should verify one specific behavior
- •Arrange-Act-Assert - Set up, execute, verify (AAA pattern)
Common Pytest Features
- •Fixtures:
@pytest.fixturefor shared setup/teardown - •Marks:
@pytest.mark.skip,@pytest.mark.slow,@pytest.mark.integration - •Mocking:
monkeypatchfixture for patching - •Temp files:
tmp_pathfixture for file system tests - •Assertions: Use
pytest.raises(ExceptionType)for exception testing