背包乱斗:福西法的宝藏免安装中文正式版
299M · 2025-10-30
pytest 是一个非常流行的 Python 测试框架,它具有强大的自动用例发现功能。理解其用例发现规则和编写用例的要求,是高效使用 pytest 的基础。
pytest 在命令行执行时(如 pytest 或 pytest tests/),会递归地搜索指定目录(默认是当前目录)下的文件和函数,自动发现哪些是测试用例。其发现规则如下:
pytest 会从你指定的路径(或当前目录)开始,递归地搜索所有子目录,除了venv虚拟环境目录和以'.'开头的目录(表示隐藏)。
pytest 会查找文件名符合test_*.py和*_test.py的 .py 文件:
在匹配的测试文件中,pytest 会查找类名以 Test 开头的类- - 例如:TestClass, TestUser, TestAPI
不包括:
- `Test` 类本身不会被实例化测试。
- 以小写 `test` 开头的类(如 `test_user`)不会被发现。
- 包含 `__init__` 方法的类通常不会被发现(除非使用 `@pytest.mark.usefixtures` 等特殊方式)。
在测试文件或 Test 类中,pytest 会查找以 test_ 开头的函数或方法-- 例如:test_login(), test_user_creation()
不包括
- `test` 函数本身不会被发现。
- `_test()`(以下划线开头)不会被发现。
- `test()`(单个test)不会被发现。
为了让 pytest 正确发现并执行测试,需要遵循:
test_*.py 或 *_test.py。test_ 前缀,这是最常见和推荐的约定。test_ 开头。test_ 开头。assert 以外的断言方式(如 self.assertEqual),虽然 unittest 风格也支持,但 pytest 推荐直接使用 Python 的 assert。Test 开头。__init__ 构造方法(除非你知道自己在做什么)。pytest 会为每个测试方法创建一个新的类实例。assert 进行断言pytest 对 Python 原生的 assert 语句进行了增强,提供了详细的失败信息(如变量值、表达式分解)。
推荐写法:
def test_addition():
assert 1 + 1 == 2
assert [1, 2, 3] == [1, 2, 3]
assert "hello" in "hello world"
print,使用 capsys 或日志虽然可以在测试中使用 print(),但 pytest 会捕获输出。
如果需要测试 print 输出,可以使用 capsys fixture:
def test_print(capsys):
print("hello")
captured = capsys.readouterr()
assert "hello" in captured.out
pytest.raises 断言异常 import pytest
def test_divide_by_zero():
with pytest.raises(ZeroDivisionError):
1 / 0
@pytest.mark 标记用例(可选) import pytest
@pytest.mark.smoke
def test_login():
...
@pytest.mark.skip(reason="Not implemented yet")
def test_logout():
...
| 项目 | 要求 |
|---|---|
| 文件名 | test_*.py 或 *_test.py |
| 类名 | 以 Test 开头(且无 __init__) |
| 函数/方法名 | 以 test_ 开头 |
| 断言 | 使用 assert 语句 |
| 异常断言 | 使用 with pytest.raises(...): |
遵循这些规则和要求,pytest 就能自动发现并运行你的测试用例。