python-pytest-patterns
Solidpytest testing patterns for Python. Triggers on: pytest, fixture, mark, parametrize, mock, conftest, test coverage, unit test, integration test, pytest.raises.
Testing & QA 400 stars
39 forks Updated today
Install
Quality Score: 85/100
Stars 20%
Recency 20%
Frontmatter 20%
Documentation 15%
Issue Health 10%
License 10%
Description 5%
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
- 7 months ago
- Last Updated
- today
- Language
- Python
- License
- None
Integrates with
Similar Skills
Semantically similar based on skill content — not just same category
Testing & QA Solid
python-testing
Python testing patterns with pytest including unit tests, integration tests, fixtures, mocking, and coverage. Use when writing Python tests.
6 Updated 4 days ago
DmitriyYukhanov Testing & QA Listed
python-testing
Python testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements.
1 Updated today
Izangi2714 Testing & QA Solid
python-testing
Python testing strategies using pytest, TDD methodology, fixtures, mocking, parametrization, and coverage requirements.
0 Updated 5 days ago
lhbsaa