Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+206 -278
View File
@@ -1,335 +1,263 @@
---
description: This rule file outlines comprehensive best practices for using pytest in Python projects, covering code organization, testing strategies, performance optimization, security measures, and common pitfalls to avoid.
globs: *.py
description: This guide defines our team's definitive best practices for writing fast, reliable, and maintainable tests using pytest, ensuring consistency and high code quality across projects.
globs: **/*.py
---
# Pytest Best Practices: A Comprehensive Guide
# pytest Best Practices
This document provides a detailed guide to using pytest effectively in Python projects, covering various aspects from code organization to security considerations. It aims to provide actionable guidance for developers to improve their testing practices and build robust applications.
This document outlines our team's standard for writing `pytest` tests. Adhering to these guidelines ensures our test suite is fast, reliable, and easy to maintain, integrating seamlessly into our CI/CD pipelines.
## Library Information:
- Name: pytest
- Tags: development, testing, python
## 1. Code Organization & Structure
## 1. Code Organization and Structure
### 1.1 Project Layout
Always use the `src/` layout for your project. This prevents common import pitfalls and ensures tests run against the installed package, not local source.
A well-organized codebase is crucial for maintainability and testability. Here are best practices for structuring your pytest projects:
**✅ GOOD:**
```
.
├── pyproject.toml
├── src/
│ └── mypackage/
│ ├── __init__.py
│ └── module.py
└── tests/
├── __init__.py
└── test_module.py
```
### 1.1. Directory Structure
**❌ BAD:**
```
.
├── pyproject.toml
├── mypackage/ # Top-level package
│ ├── __init__.py
│ └── module.py
└── tests/
└── test_module.py
```
- **Separate `tests/` directory:** Keep your tests in a directory separate from your application code, typically named `tests/`. This promotes isolation and cleaner project structure.
### 1.2 Test File Naming
Name test files `test_*.py` or `*_test.py`. We prefer `test_*.py`.
Place `__init__.py` files in `tests/` and its subdirectories to treat them as Python packages, enabling structured imports and avoiding name collisions.
my_project/
├── my_app/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
├── tests/
│ ├── __init__.py
│ ├── test_module1.py
│ └── test_module2.py
└── pyproject.toml
**✅ GOOD:**
```python
# tests/test_my_feature.py
from mypackage.module import my_function
- **`src` layout (Recommended):** Consider using a `src` layout to further isolate application code from the project root. This prevents import conflicts and improves clarity.
def test_my_function_returns_expected_value():
assert my_function() == "expected"
```
my_project/
├── src/
│ └── my_app/
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
├── tests/
│ ├── __init__.py
│ ├── test_module1.py
│ └── test_module2.py
└── pyproject.toml
### 1.3 Test Naming Conventions
Prefix test functions and methods with `test_`. For classes, prefix with `Test`. Use descriptive names that read like sentences.
### 1.2. File Naming Conventions
**✅ GOOD:**
```python
def test_user_can_be_created_with_valid_email():
# ...
pass
- **`test_*.py` or `*_test.py`:** pytest automatically discovers test files matching these patterns.
- **Descriptive names:** Use clear and descriptive names for your test files to indicate what they are testing (e.g., `test_user_authentication.py`).
class TestUserService:
def test_get_user_by_id_returns_correct_user():
# ...
pass
```
### 1.3. Module Organization
**❌ BAD:**
```python
def create_user(): # Not discovered by pytest
pass
- **Mirror application structure:** Structure your test modules to mirror the structure of your application code. This makes it easier to locate tests for specific modules.
- **`__init__.py`:** Include `__init__.py` files in your test directories to ensure they are treated as Python packages.
def test_create_user_and_email(): # Tests multiple things
pass
### 1.4. Component Architecture
class UserServiceTest: # Incorrect class prefix
pass
```
- **Isolate components:** Design your application with well-defined components that can be tested independently.
- **Dependency injection:** Use dependency injection to provide components with their dependencies, making it easier to mock and stub external resources during testing.
## 2. Common Patterns & Anti-patterns
### 1.5. Code Splitting
### 2.1 Single Assert Per Test
Each test should verify one specific behavior. This improves readability and simplifies debugging.
- **Small, focused functions:** Break down large functions into smaller, focused functions that are easier to test.
- **Modular design:** Organize your code into modules with clear responsibilities.
**✅ GOOD:**
```python
def test_user_creation_sets_email():
user = create_user("test@example.com")
assert user.email == "test@example.com"
## 2. Common Patterns and Anti-patterns
def test_user_creation_sends_welcome_email():
user = create_user("test@example.com")
assert email_service.last_sent_email == "test@example.com"
```
### 2.1. Design Patterns
**❌ BAD:**
```python
def test_user_creation_and_email_sending():
user = create_user("test@example.com")
assert user.email == "test@example.com" # First assert
send_welcome_email(user)
assert email_service.last_sent_email == "test@example.com" # Second assert
```
- **Arrange-Act-Assert (AAA):** Structure your tests following the AAA pattern for clarity.
- **Arrange:** Set up the test environment and prepare any necessary data.
- **Act:** Execute the code being tested.
- **Assert:** Verify that the code behaved as expected.
### 2.2 Fixtures for Setup/Teardown & Dependency Injection
Use fixtures to provide isolated, reusable resources and manage setup/teardown.
python
def test_example():
# Arrange
data = ...
expected_result = ...
* **Scope**:
* `function` (default): Run once per test function. Use for most tests.
* `class`: Run once per test class.
* `module`: Run once per test module. Use for expensive setups shared across module tests.
* `session`: Run once per test session. Use for global, very expensive resources (e.g., database connection pool).
* **Teardown**: Use `yield` in fixtures for automatic teardown.
* **Modularity**: Compose fixtures by injecting them into others.
* **Avoid `autouse=True`**: Only use for truly global, non-interfering setups (e.g., patching a common library function for all tests).
# Act
result = function_under_test(data)
**✅ GOOD:**
```python
# conftest.py
import pytest
# Assert
assert result == expected_result
@pytest.fixture(scope="function")
def db_session():
# Setup: Create a fresh database session
session = create_test_db_session()
yield session # Test runs here
# Teardown: Close and clean up the session
session.close()
- **Fixture factory:** Use fixture factories to create reusable test data.
@pytest.fixture(scope="module")
def api_client(db_session): # Fixtures can use other fixtures
# Setup: Create an API client with a specific DB session
client = TestClient(db_session)
yield client
# Teardown: Cleanup client resources
```
python
import pytest
**❌ BAD:**
```python
# test_bad.py
class TestUser:
def setup_method(self): # unittest-style setup, less flexible
self.user = User("test@example.com")
@pytest.fixture
def user_factory():
def create_user(username, email):
return {"username": username, "email": email}
return create_user
def test_user_email(self):
assert self.user.email == "test@example.com"
def test_create_user(user_factory):
user = user_factory("testuser", "test@example.com")
assert user["username"] == "testuser"
@pytest.fixture(autouse=True) # Avoid unless absolutely necessary
def global_patch():
# This fixture runs for *every* test, potentially causing unexpected side effects.
pass
```
### 2.2. Recommended Approaches
### 2.3 Parameterization
Use `@pytest.mark.parametrize` for data-driven tests.
- **Use fixtures for setup and teardown:** Fixtures help manage test dependencies and ensure a clean test environment.
- **Parameterize tests:** Use `@pytest.mark.parametrize` to run the same test with different inputs and expected outputs, reducing code duplication.
- **Use descriptive names for tests and fixtures:** This makes it easier to understand the purpose of each test and fixture.
- **Single Assertion per Test:** A single assertion per test makes it easier to identify the specific failure point.
**✅ GOOD:**
```python
import pytest
### 2.3. Anti-patterns and Code Smells
@pytest.mark.parametrize("input, expected_output", [
(1, 2),
(2, 3),
(0, 1),
])
def test_increment_function(input, expected_output):
assert increment(input) == expected_output
```
- **Over-reliance on fixtures:** Avoid creating too many fixtures, especially for simple data. Use direct data definition in the test if it's not reused.
- **Implicit dependencies:** Make dependencies explicit by passing them as arguments to your functions and tests.
- **Testing implementation details:** Focus on testing the behavior of your code, not the implementation details. This makes your tests more resilient to refactoring.
- **Skipping Tests Without a Reason:** Don't skip tests without a valid reason or comment explaining why.
### 2.4 Markers
Use `@pytest.mark.skip` for tests that are not yet ready or temporarily broken. Use `@pytest.mark.xfail` for known bugs that are expected to fail but should still be run.
### 2.4. State Management
**✅ GOOD:**
```python
import pytest
- **Stateless tests:** Ensure your tests are stateless and independent to avoid unexpected side effects. Each test should set up its own data and clean up after itself.
- **Fixture scopes:** Use fixture scopes (`session`, `module`, `function`) to control the lifecycle of fixtures and manage state effectively.
@pytest.mark.skip(reason="Feature not implemented yet")
def test_new_feature_endpoint():
pass
### 2.5. Error Handling
- **Test exception handling:** Write tests to verify that your code handles exceptions correctly.
python
import pytest
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def test_divide_by_zero():
with pytest.raises(ValueError) as e:
divide(10, 0)
assert str(e.value) == "Cannot divide by zero"
- **Use `pytest.raises`:** Use `pytest.raises` to assert that a specific exception is raised.
- **Log errors:** Ensure your application logs errors appropriately, and consider writing tests to verify that errors are logged correctly.
@pytest.mark.xfail(reason="Known bug: #1234")
def test_buggy_legacy_function():
assert legacy_function() == "fixed_output"
```
## 3. Performance Considerations
### 3.1. Optimization Techniques
### 3.1 Keep Tests Fast
Prioritize fast tests. Slow tests discourage local execution and lead to longer CI times.
* **Mock external services**: Use mocks for APIs, databases, and file systems.
* **Appropriate fixture scope**: Don't use `session` scope for resources that need to be fresh per test.
* **Small, focused tests**: Each test should do minimal work.
- **Profile slow tests:** Use the `--durations` option to identify slow tests and optimize them.
- **Parallel test execution:** Use `pytest-xdist` to run tests in parallel and reduce overall test execution time. `pip install pytest-xdist` then run `pytest -n auto`. The `auto` option utilizes all available CPU cores.
- **Caching:** Cache expensive computations to avoid redundant calculations during testing.
## 4. Mocking Strategies
### 3.2. Memory Management
Use the `mocker` fixture (from `pytest-mock`) for robust mocking. It integrates `unittest.mock` functionality with `pytest`'s fixture system.
- **Resource cleanup:** Ensure your tests clean up any resources they allocate, such as temporary files or database connections.
- **Limit fixture scope:** Use the appropriate fixture scope to minimize the lifetime of fixtures and reduce memory consumption.
**✅ GOOD:**
```python
# test_service.py
from mypackage.service import MyService
from mypackage.external_api import get_data
### 3.3. Bundle Size Optimization
def test_my_service_fetches_data(mocker):
mock_get_data = mocker.patch("mypackage.service.get_data")
mock_get_data.return_value = {"key": "mocked_value"}
- **N/A:** Pytest itself doesn't directly impact bundle sizes, but your application code should be optimized separately.
service = MyService()
result = service.fetch_and_process()
### 3.4. Lazy Loading
mock_get_data.assert_called_once()
assert result == "processed_mocked_value"
```
- **N/A:** Lazy loading is more relevant to application code than pytest itself, but can be used within fixtures if necessary to defer initialization.
**❌ BAD:**
```python
# test_service.py (using raw monkeypatch for complex mocks)
import pytest
from mypackage.service import MyService
from mypackage.external_api import get_data
## 4. Security Best Practices
def test_my_service_fetches_data_raw_monkeypatch(monkeypatch):
# monkeypatch is fine for simple cases, but mocker is better for complex mocks
# and provides more assert methods.
def mock_get_data():
return {"key": "mocked_value"}
monkeypatch.setattr("mypackage.service.get_data", mock_get_data)
### 4.1. Common Vulnerabilities
service = MyService()
result = service.fetch_and_process()
assert result == "processed_mocked_value"
```
- **Injection attacks:** Prevent injection attacks by validating and sanitizing user inputs.
- **Cross-site scripting (XSS):** Protect against XSS vulnerabilities by escaping user-generated content.
- **Authentication and authorization flaws:** Implement secure authentication and authorization mechanisms to protect sensitive data.
## 5. Coverage Patterns
### 4.2. Input Validation
Integrate `pytest-cov` to measure test coverage. Aim for high coverage, but understand that 100% coverage doesn't guarantee bug-free code. Focus on covering critical paths and edge cases.
- **Validate all inputs:** Validate all user inputs to ensure they conform to expected formats and ranges.
- **Use parameterized tests:** Use parameterized tests to test input validation logic with a variety of inputs, including edge cases and invalid values.
**✅ GOOD:**
```bash
# Run tests with coverage and generate report
pytest --cov=mypackage --cov-report=term-missing --cov-report=html
```
### 4.3. Authentication and Authorization
## 6. Type Hints
- **Test authentication:** Write tests to verify that your authentication mechanisms are working correctly.
- **Test authorization:** Write tests to verify that users only have access to the resources they are authorized to access.
Apply type hints to test code, especially for fixtures and complex test functions. This improves readability, maintainability, and enables static analysis.
### 4.4. Data Protection
**✅ GOOD:**
```python
from typing import Generator
import pytest
- **Encrypt sensitive data:** Encrypt sensitive data at rest and in transit.
- **Use secure storage:** Store sensitive data in secure storage locations with appropriate access controls.
class User:
def __init__(self, email: str):
self.email = email
### 4.5. Secure API Communication
@pytest.fixture
def new_user() -> Generator[User, None, None]:
user = User("test@example.com")
yield user
# Cleanup if necessary
- **Use HTTPS:** Always use HTTPS for API communication to protect data in transit.
- **Validate API responses:** Validate API responses to ensure they are valid and haven't been tampered with.
## 5. Testing Approaches
### 5.1. Unit Testing
- **Test individual units:** Unit tests should focus on testing individual functions, methods, or classes in isolation.
- **Mock dependencies:** Use mocking to isolate units under test from their dependencies.
### 5.2. Integration Testing
- **Test interactions:** Integration tests should focus on testing the interactions between different components of your application.
- **Use real dependencies (where appropriate):** For integration tests, it's often appropriate to use real dependencies, such as databases or external APIs, to ensure that the different components work together correctly. Consider using test containers for database and service dependencies.
### 5.3. End-to-End Testing
- **Test complete workflows:** End-to-end tests should focus on testing complete user workflows, from start to finish.
- **Use browser automation:** Use browser automation tools like Selenium or Playwright to simulate user interactions with your application.
### 5.4. Test Organization
- **Organize tests by feature:** Group tests by the feature they are testing to improve organization and maintainability.
- **Use clear naming conventions:** Use clear naming conventions for your tests and test files to indicate what they are testing.
### 5.5. Mocking and Stubbing
- **Use `mocker` fixture:** Use the `mocker` fixture provided by the `pytest-mock` plugin for mocking and stubbing.
- **Mock external dependencies:** Mock external dependencies, such as databases or APIs, to isolate your tests and prevent them from relying on external resources.
- **Use `autospec=True`:** Use `autospec=True` when mocking to ensure that your mocks have the same API as the original objects. This helps prevent errors caused by incorrect mock implementations.
python
def test_example(mocker):
mock_external_api = mocker.patch("module.external_api", autospec=True)
mock_external_api.return_value = {"data": "test data"}
## 6. Common Pitfalls and Gotchas
### 6.1. Frequent Mistakes
- **Not isolating tests:** Failing to isolate tests can lead to unpredictable results and make it difficult to debug failures.
- **Testing implementation details:** Testing implementation details makes your tests brittle and difficult to maintain.
- **Ignoring warnings:** Ignoring warnings from pytest can mask underlying problems in your tests.
### 6.2. Edge Cases
- **Empty inputs:** Test your code with empty inputs to ensure it handles them gracefully.
- **Invalid inputs:** Test your code with invalid inputs to ensure it handles them correctly and raises appropriate exceptions.
- **Boundary conditions:** Test your code with boundary conditions to ensure it handles them correctly.
### 6.3. Version-Specific Issues
- **Check release notes:** Check the release notes for each new version of pytest to be aware of any breaking changes or new features.
- **Pin dependencies:** Pin your pytest dependency to a specific version to avoid unexpected behavior caused by updates.
### 6.4. Compatibility Concerns
- **Check compatibility:** Check the compatibility of pytest with other technologies you are using, such as specific versions of Python or Django.
### 6.5. Debugging Strategies
- **Use `--pdb`:** Use the `--pdb` option to drop into the Python debugger when a test fails.
- **Use logging:** Use logging to add debugging information to your tests.
- **Simplify tests:** Simplify failing tests to isolate the cause of the failure.
## 7. Tooling and Environment
### 7.1. Recommended Development Tools
- **IDE:** Use a good IDE with pytest support, such as VS Code with the Python extension, PyCharm, or Sublime Text with the appropriate plugins.
- **pytest-watch:** Use `pytest-watch` for automatic test rerunning on file changes. `pip install pytest-watch`, then run `ptw`.
### 7.2. Build Configuration
- **Use `pyproject.toml`:** Use a `pyproject.toml` file to configure your pytest settings.
toml
[tool.pytest.ini_options]
addopts = [
"--cov=my_app",
"--cov-report term-missing",
"-v",
]
testpaths = [
"tests",
]
### 7.3. Linting and Formatting
- **Use `flake8-pytest-style`:** Use the `flake8-pytest-style` plugin to enforce pytest-specific coding standards. `pip install flake8 flake8-pytest-style`
- **Use `black` or `autopep8`:** Use a code formatter like `black` or `autopep8` to ensure consistent code formatting. `pip install black`, then run `black .`
### 7.4. Deployment
- **Include tests in your deployment pipeline:** Ensure your tests are run as part of your deployment pipeline to prevent regressions.
- **Use a dedicated test environment:** Use a dedicated test environment to avoid interfering with your production environment.
### 7.5. CI/CD Integration
- **Integrate with CI/CD:** Integrate pytest with your CI/CD system, such as GitHub Actions, GitLab CI, or Jenkins, to automatically run your tests on every commit.
Example GitHub Actions workflow (`.github/workflows/test.yml`):
name: Test
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pytest-cov flake8 flake8-pytest-style black
pip install -e . # Install your project in editable mode
- name: Lint with flake8
run: |
flake8 .
- name: Test with pytest
run: |
pytest --cov --cov-report xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
env_vars: OS,PYTHON
name: codecov-pytest
By following these best practices, you can write effective and maintainable tests with pytest, improving the quality and reliability of your Python applications.
def test_user_email_property(new_user: User) -> None:
assert new_user.email == "test@example.com"
```