pytest入门及使用
一、pytest安装和入门
1、安装和升级
- 安装命令
pip install pytest
- 升级命令
pip install -U pytest
2、第一个测试函数
def func(x): return x + 1 def test_answer(): assert func(3) == 5执行结果
$ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:6: AssertionError ============================ 1 failed in 0.12s =============================该测试返回失败报告,因为
func(3)没有返回5。
3、运行多个测试
pytest将在当前目录及其子目录中运行所有格式为test _ .py或 _test.py的文件
- 断言某个被引发的异常
import pytest def f(): raise SystemExit(1) def test_mytest(): with pytest.raises(SystemExit): f()以静默方式执行测试用例
$ pytest -q test_sysexit.py . [100%] 1 passed in 0.12s
- 将多个测试用例组合到一个类中
class TestClass: def test_one(self): x = "this" assert "h" in x def test_two(self): x = "hello" assert hasattr(x, "check")pytest遵循其Python测试发现约定来发现所有测试,因此它会找到两个带test_前缀的函数。不需要对任何子类进行子类化,但是请确保为类加上前缀,Test否则将跳过该类。我们可以简单地通过传递其文件名来运行模块:
$ pytest -q test_class.py .F [100%] ================================= FAILURES ================================= ____________________________ TestClass.test_two ____________________________ self = <test_class.TestClass object at 0xdeadbeef> def test_two(self): x = "hello" > assert hasattr(x, "check") E AssertionError: assert False E + where False = hasattr('hello', 'check') test_class.py:8: AssertionError 1 failed, 1 passed in 0.12s
二、pytest使用和调用
1、通过python -m pytest调用pytest
您可以从命令行通过Python解释器调用测试:
python -m pytest [...]但是,这和直接执行
pytest [...]命令的效果几乎是一模一样
2、pytest结束返回状态码
- Exit code 0:所有测试均已收集并成功通过
- Exit code 1:测试已收集并运行,但有些测试失败
- Exit code 2:测试执行被用户中断
- Exit code 3:执行测试时发生内部错误
- Exit code 4:pytest命令行用法错误
- Exit code 5:没有收集测试
3、获取版本,选项名称,环境变量
pytest --version # 获取版本 pytest --fixtures # 显示可用的内置函数参数 pytest -h | --help # 帮助
4、第一个(或N个)测试失败后停止
在第一个(N)次失败后停止测试过程:
pytest -x # 遇到第一个失败时,停止 pytest --maxfail==2 # 遇到第二个失败时,停止
5、指定测试或选择测试
Pytest支持从命令行运行和选择测试的几种方法。
- 在模块中运行测试
pytest test_mod.py
- 在目录中运行测试
pytest testing/
- 通过关键字表达式运行测试
pytest -k "MyClass and not method"注意:执行当前目录下,名字包含
MyClass但不包含method的测试用例,所以上面的示例将运行,TestMyClass.test_something 但不会运行TestMyClass.test_method_simple。
- 按节点ID运行测试
每个收集的测试都分配有一个唯一的nodeid名称,该名称由模块文件名后跟说明符(例如类名,函数名和参数化参数)组成,并用::字符分隔。
要在模块中运行特定的测试,请执行以下操作:
pytest test_mod.py::test_func
在命令行中指定测试方法的另一个示例:
pytest test_mod.py::TestClass::test_method
- 通过标记表达式运行测试
pytest -m slow
- 从包运行测试
pytest --pyargs pkg.testing这将导入
pkg.testing并使用其文件系统位置来查找并运行测试
6、从Python代码中调用pytest
pytest.main(["-x", "mytestdir"])注意:调用pytest.main()将导致导入您的测试及其导入的任何模块。由于python导入系统的缓存机制,pytest.main()从同一进程进行后续调用不会反映两次调用之间对这些文件的更改。因此,pytest.main()不建议从同一进程进行多次调用(例如,以重新运行测试)。
7、修改Python回溯打印
pytest --showlocals # 打印本地变量 pytest -l # 打印本地变量 pytest --tb=auto # 默认模式 pytest --tb=long # 尽可能详细的输出 pytest --tb=short # 更简短的输出 pytest --tb=line # 每个失败信息总结在一行中 pytest --tb=native # Python 标准输出 pytest --tb=no # 不打印失败信息
--full-trace是一种比--tb=long更详细的输出模式。它甚至能观察到用户打断执行(Ctrl+C)时的回溯信息,而上述六种模式默认是不输出此类信息的。
8、分析测试执行持续时间
要获取最慢的10个测试持续时间的列表:
pytest --durations=10默认情况下,除非
-vv在命令行中传递,否则pytest不会显示太短的测试持续时间(<0.01s)
三、参考
1、pytest-xdist: https://github.com/pytest-dev/pytest-xdist
2、pytest-rerunfailures:https://github.com/pytest-dev/pytest-rerunfailures
3、pytest-sugar: https://github.com/Teemu/pytest-sugar
4、pytest-assume:https://github.com/astraw38/pytest-assume
5、pytest-html:https://github.com/pytest-dev/pytest-html
6、插件市场:http://plugincompat.herokuapp.com/
7、https://github.com/luizyao/pytest-chinese-doc

浙公网安备 33010602011771号