python-pytest-patterns

Solid

pytest testing patterns for Python. Triggers on: pytest, fixture, mark, parametrize, mock, conftest, test coverage, unit test, integration test, pytest.raises.

Testing & QA 335 stars 29 forks Updated today

Install

View on GitHub

Quality Score: 85/100

Stars 20%
84
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
0
Description 5%
100

Skill Content

# Python pytest Patterns Modern pytest patterns for effective testing. ## Basic Test Structure ```python import pytest def test_basic(): """Simple assertion test.""" assert 1 + 1 == 2 def test_with_description(): """Descriptive name and docstring.""" result = calculate_total([1, 2, 3]) assert result == 6, "Sum should equal 6" ``` ## Fixtures ```python import pytest @pytest.fixture def sample_user(): """Create test user.""" return {"id": 1, "name": "Test User"} @pytest.fixture def db_connection(): """Fixture with setup and teardown.""" conn = create_connection() yield conn conn.close() def test_user(sample_user): """Fixtures injected by name.""" assert sample_user["name"] == "Test User" ``` ### Fixture Scopes ```python @pytest.fixture(scope="function") # Default - per test @pytest.fixture(scope="class") # Per test class @pytest.fixture(scope="module") # Per test file @pytest.fixture(scope="session") # Entire test run ``` ## Parametrize ```python @pytest.mark.parametrize("input,expected", [ (1, 2), (2, 4), (3, 6), ]) def test_double(input, expected): assert double(input) == expected # Multiple parameters @pytest.mark.parametrize("x", [1, 2]) @pytest.mark.parametrize("y", [10, 20]) def test_multiply(x, y): # 4 test combinations assert x * y > 0 ``` ## Exception Testing ```python def test_raises(): with pytest.raises(ValueError) as exc_info: raise ValueError("Invalid in...

Details

Author
aiskillstore
Repository
aiskillstore/marketplace
Created
5 months ago
Last Updated
today
Language
Python
License
None

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category