← ClaudeAtlas

python-testinglisted

Python testing best practices using pytest including fixtures, parametrization, mocking, coverage analysis, async testing, and test organization. Use when writing or improving Python tests.
SilantevBitcoin/Base-system-Claude · ★ 1 · Testing & QA · score 74
Install: claude install-skill SilantevBitcoin/Base-system-Claude
# Python Testing > Comprehensive Python testing patterns using pytest as the primary testing framework. ## Testing Framework Use **pytest** as the testing framework for its powerful features and clean syntax. ### Basic Test Structure ```python def test_user_creation(): """Test that a user can be created with valid data""" user = User(name="Alice", email="alice@example.com") assert user.name == "Alice" assert user.email == "alice@example.com" assert user.is_active is True ``` ### Test Discovery pytest automatically discovers tests following these conventions: - Files: `test_*.py` or `*_test.py` - Functions: `test_*` - Classes: `Test*` (without `__init__`) - Methods: `test_*` ## Fixtures Fixtures provide reusable test setup and teardown: ```python import pytest from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @pytest.fixture def db_session(): """Provide a database session for tests""" engine = create_engine("sqlite:///:memory:") Session = sessionmaker(bind=engine) session = Session() # Setup Base.metadata.create_all(engine) yield session # Teardown session.close() def test_user_repository(db_session): """Test using the db_session fixture""" repo = UserRepository(db_session) user = repo.create(name="Alice", email="alice@example.com") assert user.id is not None ``` ### Fixture Scopes ```python @pytest.fixture(scope="function") # Default: per test def user():