Pytest入门系列之pytest的基本命令

Pytest入门系列之pytest的基本命令

pytest 作为一个强大的测试框架,提供了非常丰富的命令参数,这些命令在我们日常工作中可以大大的帮助到我们!

运行测试的基本命令

运行所有测试

1
pytest

pytest 会自动搜索当前目录及其子目录中所有符合 test_*.py*_test.py 规则的测试文件,并执行其中的测试函数。

运行指定测试文件

1
pytest test_sample.py

仅运行 test_sample.py 文件中的测试。

运行特定的测试类

1
pytest test_sample.py::TestClass

仅运行 test_sample.pyTestClass 内部的测试方法。

运行特定的测试函数

1
pytest test_sample.py::test_function

仅运行 test_sample.pytest_function 这个测试。

测试选择与过滤

通过 -k 关键字匹配

1
pytest -k "addition"

只运行名称包含 "addition" 的测试函数。

1
pytest -k "add or sub"

运行名称包含 addsub 的测试。

1
pytest -k "not multiplication"

排除名称包含 "multiplication"的测试。

通过 -m 运行特定标记的测试

1
pytest -m slow

仅运行 @pytest.mark.slow 标记的测试。

1
2
3
4
5
6
7
import pytest

@pytest.mark.slow
def test_slow_function():
import time
time.sleep(3)
assert True

控制测试执行

失败后立即停止

1
pytest -x

第一个测试失败后立即停止。

1
pytest --maxfail=3

允许最多 3 个测试失败,超过就停止。

只运行上次失败的测试

1
pytest --lf

仅运行上一次失败的测试(last failed)。

1
pytest --ff

先运行上次失败的测试,再运行其他测试(failed first)。

@pytest.mark.skip 无条件跳过

@pytest.mark.skip 直接跳过测试,不执行该测试函数。适用于测试暂时无意义的情况。

1
2
3
4
5
6
import pytest

@pytest.mark.skip(reason="当前功能未实现")
def test_not_implemented():
assert False # 这个测试不会运行

@pytest.mark.skipif(condition, reason=”…”) 条件跳过

skipif 允许根据特定条件决定是否跳过测试。例如:

仅在特定 Python 版本下跳过测试。
仅在 Windows 环境下跳过测试。

1
2
3
4
5
6
7
import pytest
import sys
# 跳过 Python 3.6 及以下版本
@pytest.mark.skipif(sys.version_info < (3, 7), reason="需要 Python 3.7 及以上版本")
def test_python_version():
assert sys.version_info >= (3, 7)

1
2
3
4
# 仅在 Windows 跳过
@pytest.mark.skipif(sys.platform == "win32", reason="Windows 环境下跳过")
def test_not_for_windows():
assert True

@pytest.mark.xfail 预期失败

xfail(expected failure)用于标记 已知可能失败的测试,但仍然让它运行。

测试通过:显示 XPASS(意外通过)。
测试失败:显示 XFAIL(符合预期的失败)。

1
2
3
4
5
import pytest

@pytest.mark.xfail(reason="这个 bug 还没修复")
def test_known_bug():
assert 1 + 1 == 3 # 预期失败

不会影响整体测试通过率(即便失败,pytest 也不会将其视为真正的失败)。

xfail 结合 condition

1
2
3
4
@pytest.mark.xfail(sys.platform == "win32", reason="Windows 下此功能未修复")
def test_windows_bug():
assert False

仅在 Windows 下 xfail,其他平台不会受影响

strict=True 让 XPASS 变成失败

默认情况下,如果 xfail 标记的测试 意外通过(XPASS),pytest 仍认为测试通过。但可以使用 strict=True 强制失败:

1
2
3
@pytest.mark.xfail(reason="Bug 尚未修复", strict=True)
def test_should_fail():
assert 2 + 2 == 4 # 实际上会通过

详细度与输出控制

增加详细信息

1
pytest -v

显示更详细的测试结果,包括每个测试的名称和状态。

1
pytest -q

只显示必要的信息(安静模式)。

显示 print 语句输出

1
pytest -s

允许测试过程中 print() 的输出(默认 pytest 会捕获并隐藏)。

配置失败信息

控制回溯(traceback)信息

1
pytest --tb=short

显示精简的错误信息。

1
pytest --tb=long

显示完整的回溯信息(默认)。

1
pytest --tb=line

只显示一行错误信息。

1
pytest --tb=no

不显示错误回溯信息。

运行速度与性能优化

只收集测试但不执行

1
pytest --collect-only

仅收集测试并列出它们,不执行测试。

1
pytest --durations=5

显示执行最慢的 5 个测试。

1
pytest --durations=0

显示所有测试的执行时间,按慢速排序。

失败重试

失败时自动重试

1
2
pip install pytest-rerunfailures
pytest --reruns 3

失败的测试最多重试 3 次。

1
2
3
4
5
6
import pytest

@pytest.mark.flaky(reruns=3, reruns_delay=2)
def test_flaky():
import random
assert random.choice([True, False])

@pytest.mark.flaky 可用于单独标记某些不稳定的测试进行重试。

配置 pytest

使用 pytest.ini 配置默认参数

1
2
3
4
5
6
[pytest]
addopts = -v --maxfail=2 --tb=short
python_files = test_*.py *_test.py
python_classes = Test*
python_functions = test_*

addopts 指定默认运行参数。
python_files 定义测试文件格式。
python_classes 定义测试类的命名规则。
python_functions 定义测试函数的命名规则。

高级功能

并行运行测试

1
2
pip install pytest-xdist
pytest -n 4

-n 4 表示同时运行 4 个测试进程,提高测试执行速度。

总结

pytest 常用命令参数总结

参数 作用
pytest 运行所有测试
pytest test_sample.py 运行指定文件
pytest test_sample.py::test_func 运行指定测试函数
pytest -k "关键字" 只运行包含关键字的测试
pytest -m 标记 运行特定 @pytest.mark.<标记> 测试
pytest -x 第一个失败的测试后停止
pytest --lf 仅运行上次失败的测试
pytest --ff 先运行失败的测试
pytest -v 显示详细信息
pytest -s 显示 print 输出
pytest --tb=short 简化错误回溯
pytest --durations=5 显示最慢的 5 个测试
pytest --reruns 3 失败重试 3 次
pytest --junitxml=report.xml 生成 JUnit 格式报告
pytest --alluredir=./allure-results 生成 Allure 报告
pytest -n 4 并行运行 4 个测试

pytest skipskipifxfail 参数对比总结

标记 作用 适用场景 示例
@pytest.mark.skip 无条件跳过 功能未实现、不适用的测试 @pytest.mark.skip(reason="功能未实现")
@pytest.mark.skipif(condition, reason="…") 条件跳过 根据环境、依赖决定是否跳过 @pytest.mark.skipif(sys.platform == "win32", reason="Windows 下跳过")
@pytest.mark.xfail 预期失败(但仍运行) 记录已知 Bug,不影响测试通过率 @pytest.mark.xfail(reason="Bug 未修复")
@pytest.mark.xfail(strict=True) 预期失败(XPASS 视为失败) 确保 Bug 未被误修复 @pytest.mark.xfail(reason="Bug 未修复", strict=True)

Pytest入门系列之pytest的基本命令
https://dreamshao.github.io/2025/03/19/pytest入门系列2/
作者
Yun Shao
发布于
2025年3月19日
许可协议