LLM Skills
~/catalog/testing & quality//SKILL
Testing & qualityGitHub source

Python testing (pytest)

/SKILL

> This skill provides comprehensive Python testing patterns using pytest as the primary testing framework.

affaan-maffaan-m
240.5k
June 4, 2026
MIT
// skill content

--- name: python-testing description: > Python testing best practices using pytest including fixtures, parametrization, mocking, coverage analysis, async testing, and test organization. Use when writing or improving Python tests. metadata: origin: ECC globs: ["*/.py", "*/.pyi"] --- # Python Testing > This skill provides 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(): return User(name="Alice") @pytest.fixture(scope="class") # Per test class def database(): db = Database() db.connect() yield db db.disconnect() @pytest.fixture(scope="module") # Per module def app(): return create_app() @pytest.fixture(scope="session") # Once per test session def config(): return load_config() ` ### Fixture Dependencies `python @pytest.fixture def database(): db = Database() db.connect() yield db db.disconnect() @pytest.fixture def user_repository(database): """Fixture that depends on database fixture""" return UserRepository(database) def test_create_user(user_repository): user = user_repository.create(name="Alice") assert user.id is not None ` ## Parametrization Test multiple inputs with @pytest.mark.parametrize: `python import pytest @pytest.mark.parametrize("email,expected", [ ("user@example.com", True), ("invalid-email", False), ("", False), ("user@", False), ("@example.com", False), ]) def test_email_validation(email, expected): result = validate_email(email) assert result == expected ` ### Multiple Parameters `python @pytest.mark.parametrize("name,age,valid", [ ("Alice", 25, True), ("Bob", 17, False), ("", 25, False), ("Charlie", -1, False), ]) def test_user_validation(name, age, valid): result = validate_user(name, age) assert result == valid ` ### Parametrize with IDs `python @pytest.mark.parametrize("input,expected", [ ("hello", "HELLO"), ("world", "WORLD"), ], ids=["lowercase", "another_lowercase"]) def test_uppercase(input, expected): assert input.upper() == expected ` ## Test Markers Use markers for test categorization and selective execution: `python import pytest @pytest.mark.unit def test_calculate_total(): """Fast unit test""" assert calculate_total([1, 2, 3]) == 6 @pytest.mark.integration def test_database_connection(): """Slower integration test""" db = Database() assert db.connect() is True @pytest.mark.slow def test_large_dataset(): """Very slow test""" process_million_records() @pytest.mark.skip(reason="Not implemented yet") def test_future_feature(): pass @pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10+") def test_new_syntax(): pass ` **Run specific markers:** `bash pytest -m unit # Run only unit tests pytest -m "not slow" # Skip slow tests pytest -m "unit or integration" # Run unit OR integration ` ## Mocking ### Using unittest.mock ``python from unittest.mock import Mock, patch, MagicMock def testuserservicewithmock(): """Test with mock repository""" mockrepo = Mock() mockrepo.findbyid.returnvalue = User(id="1", name="Alice") service = UserService(mockrepo) user = service.getuser("1") assert user.name == "Alice" mockrepo.findbyid.assertcalledoncewith("1") @patch('myapp.services.EmailService') def testsendnotification(mockemailservice): """Test with patched dependency""" service = NotificationService() service.send("user@example.com", "Hello") mockemail_

// original public source
affaan-m/ECC
/.kiro/skills/python-testing/SKILL.md
License: MIT
Independent project, not affiliated with Anthropic. This skill remains the property of its original author.
// install this skill
Paste this command in your terminal at the root of your project:
mkdir -p .claude/commands && curl -o ".claude/commands/SKILL.md" "https://raw.githubusercontent.com/affaan-m/ECC/main/.kiro/skills/python-testing/SKILL.md"
Then in Claude Code, type /SKILL to activate it.
open_in_newOpen original source
// save
Save available after sign in.
loginSign in to save
// information
Creatoraffaan-m
Stars 240.5k
LicenseMIT
UpdatedJune 4, 2026
Format.md
AccessFree
// similar

Skills Testing & quality

View allarrow_forward