python -m pytest
进行测试我们可以从命令行通过Python解释器调用测试:
python -m pytest [...]
这基本上等同于直接调用命令行脚本,出了那在python中调用,也可将将之添加到sys.path中
运行pytest
可以产生六种不同的退出代码
退出代码为0: 收集并成功通过所有测试
退出代码为1: 收集并运行测试但一些测试失败
退出代码为2: 用户终端了测试执行
退出代码为3: 执行测试时发生内部错误
退出代码为4: pytest命令行使用错误
退出代码为5: 没有收集任何测试
它们由_pytest.main.ExitCode枚举表示.可以使用以下命令直接导入和访问作为公共API一部分的退出代码
from pytest import ExitCode
在模块中运行测试
pytest test_mod.py
在目录中运行测试
pytest testing/
按关键字表达式运行测试
pytest -k "MyClass and not method"
这将运行包括与给定字符串表达式匹配的名称的测试,该表达式可以包括使用文件名,类名和函数名作为变量的Python运算符.
以上示例将运行TestMyClass.test_something
但不会运行TestMyClass.test_method_simple
.
每个收集的测试都分配一个唯一的nodeid,包括模块文件名,后面跟说明符,比如类名,函数名和参数化参数,用::
字符分隔
pytest test_mod.py::test_func
另一个在命令中指定测试方法的示例:
pytest test_mod.py::TestClass::test_method
pytest -m slow
将运行@pytest.mark.slow
装饰器修饰的所有测试.
pytest --pyargs.pkg.testing
这将导入pkg.testing
并使用其文件系统位置来查找和运行测试
修改回溯打印的示例:
pytest --showlocals # show local variables in tracebacks
pytest -l # show local variables (shortcut)
pytest --tb=auto # (default) 'long' tracebacks for the first and last
# entry, but 'short' style for the other entries
pytest --tb=long # exhaustive, informative traceback formatting
pytest --tb=short # shorter traceback format
pytest --tb=line # only one line per failure
pytest --tb=native # Python standard library formatting
pytest --tb=no # no traceback at all
-r
标志可用于在测试会话结束时显示’简短测试摘要信息’,使大型测试套件可以轻松获得所有故障,跳过,xfails等的清晰图像.
例:
# content of test_example.py
import pytest
@pytest.fixture
def error_fixture():
assert 0
def test_ok():
print("ok")
def test_fail():
assert 0
def test_error(error_fixture):
pass
def test_skip():
pytest.skip("skipping this test")
def test_xfail():
pytest.xfail("xfailing this test")
@pytest.mark.xfail(reason="always xfail")
def test_xpass():
pass
测试后
==================================================== test session starts =====================================================
platform darwin -- Python 2.7.10, pytest-4.6.5, py-1.8.0, pluggy-0.13.0
rootdir: /Users/admin/Desktop/pytest_zzz
collected 6 items
xfails_test.py .FEsxX [100%]
=========================================================== ERRORS ===========================================================
________________________________________________ ERROR at setup of test_error ________________________________________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
xfails_test.py:7: AssertionError
========================================================== FAILURES ==========================================================
_________________________________________________________ test_fail __________________________________________________________
def test_fail():
> assert 0
E assert 0
xfails_test.py:15: AssertionError
================================================== short test summary info ===================================================
SKIPPED [1] /Users/admin/Desktop/pytest_zzz/xfails_test.py:24: skipping this test
XFAIL xfails_test.py::test_xfail
reason: xfailing this test
XPASS xfails_test.py::test_xpass always xfail
ERROR xfails_test.py::test_error - assert 0
FAILED xfails_test.py::test_fail - assert 0
======================== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.04 seconds ========================
描述-r
在它之后选项接收数量的字符,使用a
的意思是’所有排除被通过’
这里是可以使用的可用字符的完整列表
f
===>失败E
===>错误s
===>跳过x
===>xfailedX
===>xpassedp
===>过去了p
===>通过输出a
===>除了pP
A
===>全部可以使用多个字符,例如,只能查看失败和跳过的测试,我们可以执行
$ pytest -rfs
=========================== 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 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
========================= short test summary info ==========================
FAILED test_example.py::test_fail - assert 0
SKIPPED [1] $REGENDOC_TMPDIR/test_example.py:23: skipping this test
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
使用p
列表通过测试,同时P
添加额外的’PASSES’部分,其中包含已通过但已捕获输出的测试:
$ pytest -rpP
=========================== 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 6 items
test_example.py .FEsxX [100%]
================================== ERRORS ==================================
_______________________ ERROR at setup of test_error _______________________
@pytest.fixture
def error_fixture():
> assert 0
E assert 0
test_example.py:6: AssertionError
================================= FAILURES =================================
________________________________ test_fail _________________________________
def test_fail():
> assert 0
E assert 0
test_example.py:14: AssertionError
================================== PASSES ==================================
_________________________________ test_ok __________________________________
--------------------------- Captured stdout call ---------------------------
ok
========================= short test summary info ==========================
PASSED test_example.py::test_ok
== 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.12s ===
pytest
允许用户通过命令行选项在每次测试开始时立即进入PDB提示符:
pytest --trace
这将在每次测试开始时调用Python调试器
要在代码中设置断点,请在代码中使用本机Python调用,pytest会自动禁用该测试的输出捕获:import pdb;pdb.set_trace()
要获得最慢的10个测试持续时间的列表:
pytest --durations=10