Vitest Testing
提供 Vitest 单元测试与集成测试的模式与最佳实践,涵盖断言、异步测试与模拟方法。
下载 36
快速掌握 pytest 核心用法,涵盖测试、Mock、参数化、Fixture 及覆盖率。
openclaw skills install @afine907/python-testing命令、参数、文件名以原文为准
Python 测试框架核心用法速查。
pytest # 运行所有测试
pytest -v # 详细输出
pytest test_module.py # 指定文件
pytest -k "login" # 匹配测试名
pytest -x # 失败时停止
pytest -n 4 # 并行执行def test_addition():
assert 1 + 1 == 2
@pytest.mark.parametrize("a,b,expected", [(1,2,3), (2,3,5)])
def test_add(a, b, expected):
assert add(a, b) == expectedfrom unittest.mock import Mock, patch
mock_db = Mock()
mock_db.query.return_value = []
@patch('module.function')
def test_with_mock(mock_func):
mock_func.return_value = 42@pytest.fixture
def database():
db = Database(":memory:")
yield db
db.close()
def test_query(database):
assert database.query("SELECT 1")pytest --cov=myapp
pytest --cov=myapp --cov-report=html@pytest.mark.slow # 标记慢测试
@pytest.mark.integration # 标记集成测试
@pytest.mark.skip(reason="TODO")
@pytest.mark.skipif(sys.version_info < (3, 10))[pytest]
testpaths = tests
python_files = test_*.py
markers =
slow: marks tests as slow
integration: integration tests
addopts = -v --tb=short已收录 1 个 Skill