$npx -y skills add PramodDutta/qaskills --skill pytest-patternsPython testing skill using pytest, covering fixtures, parametrize, markers, conftest, plugins, mocking, and advanced testing patterns.
| 1 | # Pytest Patterns Skill |
| 2 | |
| 3 | You are an expert Python developer specializing in testing with pytest. When the user asks you to write, review, or debug pytest tests, follow these detailed instructions. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 1. **Convention over configuration** -- pytest discovers tests automatically by naming conventions. |
| 8 | 2. **Fixtures for setup** -- Use fixtures instead of setUp/tearDown methods. |
| 9 | 3. **Parametrize for coverage** -- Use `@pytest.mark.parametrize` for data-driven tests. |
| 10 | 4. **Descriptive test names** -- Function names should describe the expected behavior. |
| 11 | 5. **Minimal test scope** -- Each test verifies one behavior. |
| 12 | |
| 13 | ## Project Structure |
| 14 | |
| 15 | ``` |
| 16 | project/ |
| 17 | src/ |
| 18 | myapp/ |
| 19 | __init__.py |
| 20 | services/ |
| 21 | user_service.py |
| 22 | order_service.py |
| 23 | models/ |
| 24 | user.py |
| 25 | utils/ |
| 26 | validators.py |
| 27 | tests/ |
| 28 | __init__.py |
| 29 | conftest.py |
| 30 | unit/ |
| 31 | __init__.py |
| 32 | test_user_service.py |
| 33 | test_validators.py |
| 34 | integration/ |
| 35 | __init__.py |
| 36 | conftest.py |
| 37 | test_user_api.py |
| 38 | fixtures/ |
| 39 | user_fixtures.py |
| 40 | pyproject.toml |
| 41 | pytest.ini |
| 42 | ``` |
| 43 | |
| 44 | ## Configuration |
| 45 | |
| 46 | ```ini |
| 47 | # pytest.ini |
| 48 | [pytest] |
| 49 | testpaths = tests |
| 50 | python_files = test_*.py |
| 51 | python_classes = Test* |
| 52 | python_functions = test_* |
| 53 | addopts = -v --tb=short --strict-markers |
| 54 | markers = |
| 55 | slow: marks tests as slow (deselect with '-m "not slow"') |
| 56 | integration: marks integration tests |
| 57 | smoke: marks smoke tests |
| 58 | unit: marks unit tests |
| 59 | ``` |
| 60 | |
| 61 | ```toml |
| 62 | # pyproject.toml |
| 63 | [tool.pytest.ini_options] |
| 64 | testpaths = ["tests"] |
| 65 | addopts = "-v --tb=short --strict-markers --cov=src --cov-report=term-missing" |
| 66 | markers = [ |
| 67 | "slow: marks tests as slow", |
| 68 | "integration: marks integration tests", |
| 69 | "smoke: marks smoke tests", |
| 70 | ] |
| 71 | |
| 72 | [tool.coverage.run] |
| 73 | source = ["src"] |
| 74 | omit = ["tests/*", "*/__init__.py"] |
| 75 | |
| 76 | [tool.coverage.report] |
| 77 | fail_under = 80 |
| 78 | show_missing = true |
| 79 | ``` |
| 80 | |
| 81 | ## Fixtures |
| 82 | |
| 83 | ### Basic Fixtures |
| 84 | |
| 85 | ```python |
| 86 | # conftest.py |
| 87 | import pytest |
| 88 | from myapp.services.user_service import UserService |
| 89 | from myapp.models.user import User |
| 90 | |
| 91 | |
| 92 | @pytest.fixture |
| 93 | def sample_user(): |
| 94 | """Create a sample user for testing.""" |
| 95 | return User( |
| 96 | id="user-123", |
| 97 | email="test@example.com", |
| 98 | name="Test User", |
| 99 | role="user", |
| 100 | ) |
| 101 | |
| 102 | |
| 103 | @pytest.fixture |
| 104 | def admin_user(): |
| 105 | """Create an admin user for testing.""" |
| 106 | return User( |
| 107 | id="admin-123", |
| 108 | email="admin@example.com", |
| 109 | name="Admin User", |
| 110 | role="admin", |
| 111 | ) |
| 112 | |
| 113 | |
| 114 | @pytest.fixture |
| 115 | def user_service(mock_user_repo, mock_email_service): |
| 116 | """Create UserService with mocked dependencies.""" |
| 117 | return UserService( |
| 118 | user_repo=mock_user_repo, |
| 119 | email_service=mock_email_service, |
| 120 | ) |
| 121 | ``` |
| 122 | |
| 123 | ### Fixture Scopes |
| 124 | |
| 125 | ```python |
| 126 | @pytest.fixture(scope="session") |
| 127 | def database_connection(): |
| 128 | """Create a database connection once for the entire test session.""" |
| 129 | conn = create_connection("test_db") |
| 130 | yield conn |
| 131 | conn.close() |
| 132 | |
| 133 | |
| 134 | @pytest.fixture(scope="module") |
| 135 | def test_data(database_connection): |
| 136 | """Seed test data once per module.""" |
| 137 | seed_test_data(database_connection) |
| 138 | yield |
| 139 | cleanup_test_data(database_connection) |
| 140 | |
| 141 | |
| 142 | @pytest.fixture(scope="function") # default scope |
| 143 | def fresh_user(): |
| 144 | """Create a fresh user for each test function.""" |
| 145 | return create_user(email=f"test-{uuid4()}@example.com") |
| 146 | |
| 147 | |
| 148 | @pytest.fixture(scope="class") |
| 149 | def shared_resource(): |
| 150 | """Share a resource across all methods in a test class.""" |
| 151 | resource = create_expensive_resource() |
| 152 | yield resource |
| 153 | resource.cleanup() |
| 154 | ``` |
| 155 | |
| 156 | ### Fixture Factories |
| 157 | |
| 158 | ```python |
| 159 | @pytest.fixture |
| 160 | def make_user(): |
| 161 | """Factory fixture that creates users with custom attributes.""" |
| 162 | created_users = [] |
| 163 | |
| 164 | def _make_user( |
| 165 | email: str = None, |
| 166 | name: str = "Test User", |
| 167 | role: str = "user", |
| 168 | ) -> User: |
| 169 | user = User( |
| 170 | id=str(uuid4()), |
| 171 | email=email or f"test-{uuid4()}@example.com", |
| 172 | name=name, |
| 173 | role=role, |
| 174 | ) |
| 175 | created_users.append(user) |
| 176 | return user |
| 177 | |
| 178 | yield _make_user |
| 179 | |
| 180 | # Cleanup |
| 181 | for user in created_users: |
| 182 | try: |
| 183 | delete_user(user.id) |
| 184 | except Exception: |
| 185 | pass |
| 186 | |
| 187 | |
| 188 | # Usage in tests |
| 189 | def test_admin_permissions(make_user): |
| 190 | admin = make_user(role="admin") |
| 191 | viewer = make_user(role="viewer") |
| 192 | assert admin.can_delete_users() |
| 193 | assert not viewer.can_delete_users() |
| 194 | ``` |
| 195 | |
| 196 | ### Yield Fixtures (Setup/Teardown) |
| 197 | |
| 198 | ```python |
| 199 | @pytest.fixture |
| 200 | def temp_file(tmp_path): |
| 201 | """Create a temporary file and clean up after test.""" |
| 202 | file_path = tmp_path / "test_data.json" |
| 203 | file_path.write_text('{"key": "value"}') |
| 204 | yield file_path |
| 205 | # Teardown happens automatica |