重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
pytest-advanced by laurigates/claude-plugins
npx skills add https://github.com/laurigates/claude-plugins --skill pytest-advanced用于构建健壮、可维护测试套件的高级 pytest 功能。
| 使用此技能当... | 使用 python-testing 替代当... |
|---|---|
| 编写具有作用域/工厂模式的夹具时 | 基础测试结构问题 |
| 使用复杂数据进行参数化时 | 简单的断言模式 |
| 设置 pytest 插件(cov、xdist)时 | 运行现有测试 |
| 组织 conftest.py 层级结构时 | 测试发现基础 |
| 使用 pytest-asyncio 进行异步测试时 | 同步单元测试 |
# Core + common plugins
uv add --dev pytest pytest-cov pytest-asyncio pytest-xdist pytest-mock
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = [
"-v",
"--strict-markers",
"--tb=short",
"-ra",
"--cov=src",
"--cov-report=term-missing",
"--cov-fail-under=80",
]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"integration: marks tests as integration tests",
]
asyncio_mode = "auto"
[tool.coverage.run]
branch = true
source = ["src"]
[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "@abstractmethod"]
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
import pytest
from typing import Generator
# function(默认)- 每个测试都重新创建
@pytest.fixture
def db() -> Generator[Database, None, None]:
database = Database(":memory:")
database.create_tables()
yield database
database.close()
# session - 在所有测试间共享
@pytest.fixture(scope="session")
def app():
return create_app("testing")
# autouse - 自动应用
@pytest.fixture(autouse=True)
def reset_state():
clear_cache()
yield
| 作用域 | 生命周期 |
|---|---|
function | 每个测试(默认) |
class | 每个测试类 |
module | 每个测试文件 |
session | 整个测试运行 |
@pytest.fixture(params=["sqlite", "postgres", "mysql"])
def database_backend(request) -> str:
return request.param # 测试运行3次
# 间接参数化
@pytest.fixture
def user(request) -> User:
return User(**request.param)
@pytest.mark.parametrize("user", [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
], indirect=True)
def test_user_validation(user: User):
assert user.name
@pytest.fixture
def user_factory() -> Callable[[str], User]:
created: list[User] = []
def _create(name: str, **kwargs) -> User:
user = User(name=name, **kwargs)
created.append(user)
return user
yield _create
for u in created:
u.delete()
# 内置标记
@pytest.mark.skip(reason="Not implemented")
@pytest.mark.skipif(sys.version_info < (3, 12), reason="Requires 3.12+")
@pytest.mark.xfail(reason="Known bug #123")
@pytest.mark.timeout(10)
# 参数化
@pytest.mark.parametrize("input,expected", [
pytest.param(2, 4, id="two"),
pytest.param(3, 9, id="three"),
pytest.param(-2, 4, id="negative"),
])
def test_square(input: int, expected: int):
assert input ** 2 == expected
# 按标记运行
pytest -m unit # 仅单元测试
pytest -m "not slow" # 跳过慢速测试
pytest -m "integration and not slow" # 组合标记
| 插件 | 用途 | 关键命令 |
|---|---|---|
| pytest-cov | 覆盖率 | pytest --cov=src --cov-report=term-missing |
| pytest-xdist | 并行 | pytest -n auto |
| pytest-asyncio | 异步测试 | asyncio_mode = "auto" 在配置中 |
| pytest-mock | 模拟 | mocker 夹具 |
| pytest-timeout | 超时 | @pytest.mark.timeout(10) |
@pytest.mark.asyncio
async def test_async_function():
result = await fetch_data()
assert result is not None
@pytest.fixture
async def async_client() -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient() as client:
yield client
def test_with_mock(mocker):
mock_api = mocker.patch("myapp.external.api_call")
mock_api.return_value = {"data": "test"}
result = my_function()
assert result["data"] == "test"
mock_api.assert_called_once()
# 执行
pytest # 所有测试
pytest tests/test_models.py::test_user # 特定测试
pytest -k "user and not slow" # 模式匹配
# 并行
pytest -n auto # 所有CPU
pytest -n 4 # 4个工作进程
# 覆盖率
pytest --cov=src --cov-report=html --cov-report=term-missing
# 失败测试
pytest --lf # 仅上次失败的
pytest --ff # 先运行失败的
pytest -x # 在首次失败时停止
pytest --maxfail=3 # 在3次失败后停止
# 调试
pytest -x --pdb # 失败时调试
pytest -s # 显示打印输出
pytest --collect-only # 试运行
# .github/workflows/test.yml
- name: Run tests
run: |
uv run pytest \
--cov=src \
--cov-report=xml \
--cov-report=term-missing \
--junitxml=test-results.xml
| 场景 | 命令 |
|---|---|
| 快速检查 | pytest -x --tb=short -q |
| 快速失败 | pytest -x --maxfail=1 --tb=short |
| 并行快速 | pytest -n auto -x --tb=short -q |
| 特定测试 | pytest tests/test_foo.py::test_bar -v |
| 按标记 | pytest -m "not slow" -x --tb=short |
| 覆盖率检查 | pytest --cov=src --cov-fail-under=80 -q |
| CI 模式 | pytest --junitxml=results.xml --cov-report=xml -q |
| 上次失败 | pytest --lf --tb=short |
| 调试 | pytest -x --pdb -s |
有关 conftest.py 层级结构、异步测试、测试组织和常见模式的详细模式,请参阅 REFERENCE.md。
每周安装次数
69
代码仓库
GitHub 星标数
19
首次出现
2026年1月29日
安全审计
安装于
opencode67
github-copilot67
gemini-cli66
amp66
codex66
kimi-cli66
Advanced pytest features for robust, maintainable test suites.
| Use this skill when... | Use python-testing instead when... |
|---|---|
| Writing fixtures with scopes/factories | Basic test structure questions |
| Parametrizing with complex data | Simple assert patterns |
| Setting up pytest plugins (cov, xdist) | Running existing tests |
| Organizing conftest.py hierarchy | Test discovery basics |
| Async testing with pytest-asyncio | Synchronous unit tests |
# Core + common plugins
uv add --dev pytest pytest-cov pytest-asyncio pytest-xdist pytest-mock
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = [
"-v",
"--strict-markers",
"--tb=short",
"-ra",
"--cov=src",
"--cov-report=term-missing",
"--cov-fail-under=80",
]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"integration: marks tests as integration tests",
]
asyncio_mode = "auto"
[tool.coverage.run]
branch = true
source = ["src"]
[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "@abstractmethod"]
import pytest
from typing import Generator
# function (default) - fresh per test
@pytest.fixture
def db() -> Generator[Database, None, None]:
database = Database(":memory:")
database.create_tables()
yield database
database.close()
# session - shared across all tests
@pytest.fixture(scope="session")
def app():
return create_app("testing")
# autouse - applies automatically
@pytest.fixture(autouse=True)
def reset_state():
clear_cache()
yield
| Scope | Lifetime |
|---|---|
function | Each test (default) |
class | Each test class |
module | Each test file |
session | Entire test run |
@pytest.fixture(params=["sqlite", "postgres", "mysql"])
def database_backend(request) -> str:
return request.param # Test runs 3 times
# Indirect parametrization
@pytest.fixture
def user(request) -> User:
return User(**request.param)
@pytest.mark.parametrize("user", [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
], indirect=True)
def test_user_validation(user: User):
assert user.name
@pytest.fixture
def user_factory() -> Callable[[str], User]:
created: list[User] = []
def _create(name: str, **kwargs) -> User:
user = User(name=name, **kwargs)
created.append(user)
return user
yield _create
for u in created:
u.delete()
# Built-in markers
@pytest.mark.skip(reason="Not implemented")
@pytest.mark.skipif(sys.version_info < (3, 12), reason="Requires 3.12+")
@pytest.mark.xfail(reason="Known bug #123")
@pytest.mark.timeout(10)
# Parametrize
@pytest.mark.parametrize("input,expected", [
pytest.param(2, 4, id="two"),
pytest.param(3, 9, id="three"),
pytest.param(-2, 4, id="negative"),
])
def test_square(input: int, expected: int):
assert input ** 2 == expected
# Run by marker
pytest -m unit # Only unit tests
pytest -m "not slow" # Skip slow tests
pytest -m "integration and not slow" # Combine markers
| Plugin | Purpose | Key Command |
|---|---|---|
| pytest-cov | Coverage | pytest --cov=src --cov-report=term-missing |
| pytest-xdist | Parallel | pytest -n auto |
| pytest-asyncio | Async tests | asyncio_mode = "auto" in config |
| pytest-mock | Mocking | mocker fixture |
| pytest-timeout | Timeouts | @pytest.mark.timeout(10) |
@pytest.mark.asyncio
async def test_async_function():
result = await fetch_data()
assert result is not None
@pytest.fixture
async def async_client() -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient() as client:
yield client
def test_with_mock(mocker):
mock_api = mocker.patch("myapp.external.api_call")
mock_api.return_value = {"data": "test"}
result = my_function()
assert result["data"] == "test"
mock_api.assert_called_once()
# Execution
pytest # All tests
pytest tests/test_models.py::test_user # Specific test
pytest -k "user and not slow" # Pattern matching
# Parallel
pytest -n auto # All CPUs
pytest -n 4 # 4 workers
# Coverage
pytest --cov=src --cov-report=html --cov-report=term-missing
# Failed tests
pytest --lf # Last failed only
pytest --ff # Failed first
pytest -x # Stop on first failure
pytest --maxfail=3 # Stop after 3
# Debugging
pytest -x --pdb # Debug on failure
pytest -s # Show print output
pytest --collect-only # Dry run
# .github/workflows/test.yml
- name: Run tests
run: |
uv run pytest \
--cov=src \
--cov-report=xml \
--cov-report=term-missing \
--junitxml=test-results.xml
| Context | Command |
|---|---|
| Quick check | pytest -x --tb=short -q |
| Fail fast | pytest -x --maxfail=1 --tb=short |
| Parallel fast | pytest -n auto -x --tb=short -q |
| Specific test | pytest tests/test_foo.py::test_bar -v |
| By marker | pytest -m "not slow" -x --tb=short |
| Coverage check | pytest --cov=src --cov-fail-under=80 -q |
For detailed patterns on conftest.py hierarchy, async testing, test organization, and common patterns, see REFERENCE.md.
Weekly Installs
69
Repository
GitHub Stars
19
First Seen
Jan 29, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode67
github-copilot67
gemini-cli66
amp66
codex66
kimi-cli66
通过 LiteLLM 代理让 Claude Code 对接 GitHub Copilot 运行 | 高级变通方案指南
48,700 周安装
| CI mode | pytest --junitxml=results.xml --cov-report=xml -q |
| Last failed | pytest --lf --tb=short |
| Debug | pytest -x --pdb -s |