Writes and optimizes Python test cases using pytest. Covers fixtures, parametrization, assertions, test organization, exception testing, and best practices. Activate when creating or debugging test files.
Ships in external-gitcode-ascend-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
使用pytest框架编写高质量、可维护的Python测试用例。支持简单断言、自动发现、fixtures、参数化测试等pytest核心特性。
# test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 4
运行测试:
pytest
test_*.py 或 *_test.pytest_*Test*使用简单的assert语句,pytest提供详细的失败信息:
def test_addition():
assert 1 + 1 == 2
def test_string_operations():
text = "hello"
assert text.upper() == "HELLO"
assert len(text) == 5
Fixtures用于管理测试资源和依赖关系:
import pytest
@pytest.fixture
def sample_data():
return {"name": "test", "value": 42}
def test_with_fixture(sample_data):
assert sample_data["value"] == 42
高级fixtures用法:参见 references/fixtures.md
使用@pytest.mark.parametrize减少重复代码:
@pytest.mark.parametrize("input,expected", [
(3, 4),
(5, 6),
(10, 11),
])
def test_increment(input, expected):
assert input + 1 == expected
更多参数化模式:参见 references/parametrize.md
利用pytest的断言内省功能:
def test_dict_comparison():
expected = {"a": 1, "b": 2}
actual = {"a": 1, "b": 3}
assert actual == expected # pytest会显示详细的差异
断言最佳实践:参见 references/assertions.md
tests/
├── test_auth.py
├── test_database.py
├── test_api.py
└── conftest.py # 共享fixtures
在conftest.py中定义共享fixtures:
# conftest.py
@pytest.fixture
def db_connection():
conn = create_connection()
yield conn
conn.close()
使用pytest.raises测试异常:
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
def test_value_error():
with pytest.raises(ValueError) as exc_info:
int("invalid")
assert "invalid literal" in str(exc_info.value)
@pytest.mark.skip(reason="功能未实现")
def test_not_implemented():
pass
@pytest.mark.skipif(sys.version_info < (3, 8), reason="需要Python 3.8+")
def test_python38_feature():
pass
@pytest.mark.xfail
def test_known_failure():
assert False # 预期失败
test_user_login_with_valid_credentialsdef test_calculate_total_with_discount():
# Arrange
cart = Cart(items=[Item(price=100), Item(price=50)])
discount = 0.1
# Act
total = cart.calculate_total(discount)
# Assert
assert total == 135
更多最佳实践:参见 references/best-practices.md
# 运行所有测试
pytest
# 运行特定文件
pytest tests/test_auth.py
# 运行特定测试函数
pytest tests/test_auth.py::test_login
# 运行匹配模式的测试
pytest -k "login"
# 显示详细输出
pytest -v
# 显示print语句输出
pytest -s
# 在第一个失败时停止
pytest -x
# 运行上次失败的测试
pytest --lf
# 生成覆盖率报告
pytest --cov=src
# 生成HTML报告
pytest --cov=src --cov-report=html
pytest -v --tb=long
def test_debugging():
import pdb; pdb.set_trace()
result = complex_calculation()
assert result == expected
或在命令行:
pytest --pdb
详细参考文档:
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/external-gitcode-ascend-skills:external-gitcode-ascend-pytest-writerfrom external-gitcode-ascend-skills
npx claudepluginhub ascend-ai-coding/awesome-ascend-skills --plugin external-gitcode-ascend-skills91 skills in this plugin
/external-gitcode-ascend-analyse-coverage分析测试覆盖率盲区,生成覆盖率分析报告/external-gitcode-ascend-arxiv-recommendation-npu自动化推荐系统论文发现流水线。抓取 arxiv 推荐论文,检测源码,生成待迁移任务清单,由 npu-model-migration skill 完成 NPU 适配。/external-gitcode-ascend-ascendc-operator-code-gen根据设计文档生成 AscendC 算子完整代码实现并完成框架适配。TRIGGER when: 设计文档已完成,需要生成 op_host/op_kernel 代码、注册到 PyTorch 框架、编译测试。关键词:代码生成、op_host、op_kernel、tiling、kernel、框架适配、算子注册。Other plugins' skills for the same job