ワンクリックで
pytest-setup
Setup pytest with coverage reporting and watch mode for this Python project
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Setup pytest with coverage reporting and watch mode for this Python project
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Debug code using detailed profiling and critical path timing before making suggestions
Workflow for investigating and fixing failing tests in this project
Making Python or TypeScript packages shareable (pip/npm)
Commands to execute tests in this project with various options and configurations
Format code according to project standards and fix linting issues
Git branch strategy, commit conventions, and PR process for this project
| name | pytest-setup |
| description | Setup pytest with coverage reporting and watch mode for this Python project |
| auto-activates | ["set up pytest","configure pytest","install pytest","setup test framework","configure testing"] |
This skill activates when you need to:
Check if testing is already configured:
tests/, test/, or __tests__/)pytest in requirements.txt, pyproject.toml, or PipfileIf already configured: This skill provides configuration improvements and troubleshooting.
If using pip (requirements.txt):
pip install pytest pytest-cov pytest-watch
Add to requirements.txt or requirements-dev.txt:
pytest>=7.4.0
pytest-cov>=4.1.0
pytest-watch>=4.2.0
If using Poetry:
poetry add --group dev pytest pytest-cov pytest-watch
If using Pipenv:
pipenv install --dev pytest pytest-cov pytest-watch
If using conda:
conda install pytest pytest-cov
pip install pytest-watch
# Create main test directory
mkdir -p tests
# Create __init__.py to make it a package
touch tests/__init__.py
# Create conftest.py for shared fixtures
touch tests/conftest.py
# Create your first test file
touch tests/test_example.py
Add a sample test to tests/test_example.py:
def test_basic_example():
"""Basic example test to verify pytest is working."""
assert 1 + 1 == 2
def test_string_operations():
"""Test string operations."""
assert "hello".upper() == "HELLO"
Option A: pytest.ini (Recommended)
Create pytest.ini in project root:
[pytest]
testpaths = tests
python_files = test_*.py *_test.py
python_classes = Test*
python_functions = test_*
addopts =
-v
--strict-markers
--cov=src
--cov-report=html
--cov-report=term-missing
--cov-report=xml
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
integration: marks tests as integration tests
Option B: pyproject.toml
If using pyproject.toml, add:
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"-v",
"--strict-markers",
"--cov=src",
"--cov-report=html",
"--cov-report=term-missing",
]
markers = [
"slow: marks tests as slow",
"integration: marks tests as integration tests",
]
Note: Update --cov=src to match your source directory:
--cov=src if source is in src/--cov=app if source is in app/--cov=. for current directory--cov=mypackage for package nameAdd to tests/conftest.py:
"""
Shared pytest fixtures and configuration.
"""
import pytest
@pytest.fixture
def sample_data():
"""Provide sample data for tests."""
return {
"name": "Test User",
"email": "test@example.com",
"age": 25
}
@pytest.fixture
def temp_directory(tmp_path):
"""Provide a temporary directory for tests."""
test_dir = tmp_path / "test_workspace"
test_dir.mkdir()
return test_dir
Basic test run:
pytest
Verbose output:
pytest -v
With coverage:
pytest --cov=src --cov-report=html
Run specific test file:
pytest tests/test_example.py
Run specific test:
pytest tests/test_example.py::test_basic_example
Run tests matching pattern:
pytest -k "test_string"
Skip slow tests:
pytest -m "not slow"
Run pytest-watch for continuous testing:
# Watch for changes and re-run tests
ptw
# Watch with verbose output
ptw -- -v
# Watch specific directory
ptw tests/
Alternative: pytest-xdist for parallel execution:
pip install pytest-xdist
pytest -n auto # Auto-detect CPU count
pytest -n 4 # Use 4 workers
Symptoms: pytest finds 0 tests or can't find test files
Solutions:
test_*.py or *_test.pytest_tests/ directory has __init__.pytestpaths in pytest configurationpytest --collect-only -vSymptoms: Coverage report shows 0% or doesn't include your code
Solutions:
--cov= points to correct source directory__init__.py exists in source directoriespytest --cov=. --cov-report=term to see detailed report.coveragerc or pyproject.toml coverage configSymptoms: ModuleNotFoundError when running tests
Solutions:
pip install -e .export PYTHONPATH="${PYTHONPATH}:${PWD}"conftest.py with path modifications:import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
setup.py or pyproject.toml is configured correctlySymptoms: fixture 'name' not found
Solutions:
conftest.py exists in tests/ directory@pytest.fixture decoratorSolutions:
pytest -n auto@pytest.mark.slow and skip: pytest -m "not slow"pytest --durations=10 to see slowest testspytest -vhtmlcov/ directoryAdd to .vscode/settings.json:
{
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"tests",
"-v"
]
}
GitHub Actions example:
- name: Run tests with pytest
run: |
pip install pytest pytest-cov
pytest --cov=src --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
Add to .pre-commit-config.yaml:
- repo: local
hooks:
- id: pytest-check
name: pytest
entry: pytest
language: system
pass_filenames: false
always_run: true