目录html
python -m pytest
调用pytest
pytest
执行结束时返回的状态码PDB
环境JUnitXML
格式的测试报告URL
连接 -- pastebin
服务python
代码中调用pytest
python -m pytest
调用pytest
你能够经过python的解释器来执行测试:node
python -m pytest [...]
可是,这和直接执行pytest [...]
命令的效果几乎是如出一辙的;python
pytest
执行结束时返回的状态码pytest
命令执行结束,可能会返回如下六种状态码:git
pytest
命令使用错误它们在枚举类 _pytest.main.ExitCode 中声明。而且,其做为公开API的一部分,可以直接引入和访问:github
from pytest import ExitCode
pytest --version # 查看版本号和pytest的引入路径 pytest -h # 查看帮助信息
当达到最大上限时,退出执行;如未配置,则没有上限:缓存
pytest -x # 遇到第一个失败时,退出执行 pytest --maxfail==2 # 遇到第二个失败时,退出执行
pytest
支持多种方式来执行特定的测试用例:bash
pytest test_mod.py
pytest testing/
执行当前目录下,名字包含_class
但不包含two
的测试用例:session
pytest -k "_class and not two" .
注意:python的关键字不能够应用在
-k
选项中,例如,class
、def
等。app
nodeid
的测试用例pytest
为每个收集到的测试用例指定一个惟一的nodeid
。其由模块名加说明符构成,中间以::
间隔。函数
其中,说明符能够是类名、函数名以及由parametrize
标记赋予的参数:
# src/chapter-2/test_nodeid.py import pytest def test_one(): print('test_one') assert 1 class TestNodeId: def test_one(self): print('TestNodeId::test_one') assert 1 @pytest.mark.parametrize('x,y', [(1, 1), (3, 4)]) def test_two(self, x, y): print(f'TestNodeId::test_two::{x} == {y}') assert x == y
在上述示例中,咱们建立了三个测试用例,分别对应不一样的说明符:
指定函数名执行
$ pipenv run pytest -q -s src/chapter-2/test_nodeid.py::test_one test_one . 1 passed in 0.01s
指定类名+函数名执行
$ pipenv run pytest -q -s src/chapter-2/test_nodeid.py::TestNodeId::test_one TestNodeId::test_one . 1 passed in 0.01s
指定由parametrize
标记赋予的参数执行
$ pipenv run pytest -q -s src/chapter-2/test_nodeid.py::TestNodeId::test_two[1-1] TestNodeId::test_two::1 == 1 . 1 passed in 0.01s
这里对参数x
、y
赋值的形式是[1-1]
,中间以-
间隔;
单个或多个参数的赋值形式以此类比;而且,只能为[1-1]
或者[3-4]
,其它的会报错;
注意:
这里咱们也可使用
-k
选项达到一样的效果:
首先,可使用
--collect-only
选项查看用例名:λ pipenv run pytest -q -s --collect-only src/chapter-2/test_nodeid.py test_nodeid.py::test_one test_nodeid.py::TestNodeId::test_one test_nodeid.py::TestNodeId::test_two[1-1] test_nodeid.py::TestNodeId::test_two[3-4]而后,使用
-k
执行符合规则的用例,例如:执行test_nodeid.py::test_one
:λ pipenv run pytest -q -s -k 'test_one and not TestNodeId' src/chapter-2/test_nodeid.py test_one . 1 passed, 3 deselected in 0.02s结果和执行
pipenv run pytest -q -s src/chapter-2/test_nodeid.py::test_one
同样;
pytest -m slow
pytest --pyargs pkg.testing
pytest
会引入pkg.testing
包,并在它的系统目录下搜寻测试用例并执行;
pytest回溯信息的输出一共有六种模式:auto/long/short/line/native/no,用--tb
选项指定:
pytest -l, --showlocals # 打印本地变量 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
)时的回溯信息,而上述六种模式默认是不输出此类信息的。
-r
选项能够在执行结束后,打印一个简短的总结报告。在执行的测试用例不少时,可让你对结果有个清晰的了解:
# src/chapter-2/test_report.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
$ pipenv run pytest -q -rA src/chapter-2/test_report.py .FEsxX [100%] ================================ ERRORS ================================= _____________________ ERROR at setup of test_error ______________________ @pytest.fixture def error_fixture(): > assert 0 E assert 0 src/chapter-2/test_report.py:27: AssertionError =============================== FAILURES ================================ _______________________________ test_fail _______________________________ def test_fail(): > assert 0 E assert 0 src/chapter-2/test_report.py:35: AssertionError ================================ PASSES ================================= ________________________________ test_ok ________________________________ ------------------------- Captured stdout call -------------------------- ok ======================== short test summary info ======================== PASSED src/chapter-2/test_report.py::test_ok SKIPPED [1] /Users/yaomeng/Private/Projects/pytest-chinese-doc/src/chapter-2/test_report.py:44: skipping this test XFAIL src/chapter-2/test_report.py::test_xfail reason: xfailing this test XPASS src/chapter-2/test_report.py::test_xpass always xfail ERROR src/chapter-2/test_report.py::test_error - assert 0 FAILED src/chapter-2/test_report.py::test_fail - assert 0 1 failed, 1 passed, 1 skipped, 1 xfailed, 1 xpassed, 1 error in 0.08s
-r
选项后面要紧接这一个参数,用于过滤显示测试用例的结果。
如下是全部有效的字符参数:
print
等p
和P
的上述字符参数能够叠加使用,例如:咱们指望过滤出失败的和未执行的:
pytest -rfs
PDB
是python
内建的诊断器,pytest
容许经过如下命令在执行失败时进入这个诊断器模式:
pytest --pdb
pytest
会在测试用例失败(或者Ctrl+C
)时,调用这个诊断器:
# src/chapter-2/test_pdb.py def test_fail(): x = 1 assert x == 0
$ pipenv run pytest -q --pdb src/chapter-2/test_pdb.py F >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> def test_fail(): x = 1 > assert x == 0 E assert 1 == 0 src/chapter-2/test_pdb.py:25: AssertionError >>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> PDB post_mortem (IO-capturing turned off) >>>>>>>>>>>>>>> > /Users/yaomeng/Private/Projects/pytest-chinese-doc/src/chapter-2/test_pdb.py(25)test_fail() -> assert x == 0 (Pdb) (Pdb) x 1 (Pdb) (Pdb) import sys (Pdb) sys.last_value AssertionError('assert 1 == 0') (Pdb) sys.last_type <class 'AssertionError'> (Pdb) sys.last_traceback <traceback object at 0x1077ec808>
你还能够访问测试用例的本地变量x
;
失败的信息存储在sys.last_value, sys.last_type, sys.last_traceback
变量中,你能够在交互环境中访问它们;
使用exit
命令,退出PDB
环境;
PDB
环境经过如下命令,pytest
容许你在每一个测试用例开始执行时,就加载PDB
环境:
pytest --trace
在测试用例代码中添加import pdb;pdb.set_trace()
,当其被调用时,pytest
会中止这条用例的输出:
continue
命令,退出PDB
环境,并继续执行用例;python 3.7介绍了一个内置breakpoint()
函数。pytest能够在如下场景中支持使用:
breakpoint()
被调用,而且PYTHONBREAKPOINT
为None
时,pytest
会使用内部自定义的PDB
代替系统的;PDB
;--pdb
选项时,breakpoint()
和测试发生错误时,都会调用内部自定义的PDB
;--pdbcls
选项容许指定一个用户自定义的PDB
类;获取执行最慢的10个测试用例:
pytest --durations=10
默认状况下,pytest
不会显示执行时间<0.01s的测试用例,可使用-vv
选项查看它们;
5.0版本新增特性
在测试执行中发生段错误或者超时的状况下,faulthandler
标准模块能够转储python
的回溯信息;
它在pytest
的执行中默认使能,使用-p no:faulthandler
选项能够关闭它;
一样,faulthandler_timeout=X
配置项,可用于当测试用例的完成时间超过X
秒时,转储全部线程的python
回溯信息:
# src/chapter-2/pytest.ini [pytest] faulthandler_timeout=5
配置测试执行的超时时间是5秒;
# test_fault_handler.py import time def test_faulthandler(): time.sleep(7) assert 1
测试用例中添加等待7秒的操做;
默认使能faulthandler
的状况:
$ pipenv run pytest -q src/chapter-2/test_faulthandler.py Timeout (0:00:05)! Thread 0x000000010ff275c0 (most recent call first): File "/Users/yaomeng/Private/Projects/pytest-chinese-doc/src/chapter-2/test_faulthandler.py", line 26 in test_faulthandler File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/python.py", line 170 in pytest_pyfunc_call File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 86 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 92 in _hookexec File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__ File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/python.py", line 1423 in runtest File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 117 in pytest_runtest_call File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 86 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 92 in _hookexec File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__ File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 192 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 220 in from_call File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 192 in call_runtest_hook File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 167 in call_and_report File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 87 in runtestprotocol File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/runner.py", line 72 in pytest_runtest_protocol File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 86 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 92 in _hookexec File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__ File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/main.py", line 256 in pytest_runtestloop File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 86 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 92 in _hookexec File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__ File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/main.py", line 235 in _main File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/main.py", line 191 in wrap_session File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/main.py", line 228 in pytest_cmdline_main File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/callers.py", line 187 in _multicall File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 86 in <lambda> File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/manager.py", line 92 in _hookexec File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/pluggy/hooks.py", line 286 in __call__ File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/lib/python3.7/site-packages/_pytest/config/__init__.py", line 77 in main File "/Users/yaomeng/.local/share/virtualenvs/pytest-chinese-doc-EK3zIUmM/bin/pytest", line 10 in <module> . [100%] 1 passed in 7.02s
在执行刚超过5秒的时候会打印出回溯信息。但不会中断测试的执行;
去使能faulthandler
的状况:
$ pipenv run pytest -q -p no:faulthandler src/chapter-2/test_faulthandler.py . [100%] 1 passed in 7.02s
超时并不会触发回溯信息的打印;
注意:
这个功能是从pytest-faulthandler插件合并而来的,可是有两点不一样:
- 去使能时,使用
-p no:faulthandler
代替原来的--no-faulthandler
;- 使用
faulthandler_timeout
配置项代替--faulthandler-timeout
命令行选项来配置超时时间。固然,你也可使用-o faulthandler_timeout=X
在命令行配置;
JUnitXML
格式的测试报告使用以下命令,能够在指定的path
中建立一个能被Jenkins或者其余CI工具读取的XML
格式的测试报告:
pytest --junitxml=path
你能够在项目的pytest.ini
文件中,经过设置junit_suite_name
的值,自定义XML
文件中testsuite
根节点的name
信息:
junit_suite_name
是4.0版本新增的配置项;
# src/chapter-2/pytest.ini [pytest] junit_suite_name = pytest_chinese_doc
咱们来执行一个测试用例test_nodeid.py::test_one
看看效果:
pipenv run pytest -q --junitxml=src/chapter-2/report/test_one.xml src/chapter-2/test_nodeid.py::test_one
生成的XML
测试报告:
<?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite errors="0" failures="0" hostname="NJ-LUYAO-T460" name="pytest_chinese_doc" skipped="0" tests="1" time="0.030" timestamp="2019-09-27T14:33:32.459788"> <testcase classname="test_nodeid" file="test_nodeid.py" line="24" name="test_one" time="0.002"> <system-out>test_one </system-out> </testcase> </testsuite> </testsuites>
咱们能够看到,<testsuite>
节点的name
属性的值,变为咱们所指望的pytest_chinese_doc
,而不是默认的pytest
;
JUnit XML规定time
属性应该代表测试用例执行的所有耗时,包含setup
和teardown
中的操做,这也是pytest的默认行为;
若是你只想记录测试用例执行的时间,只须要作以下配置:
# src/chapter-2/pytest.ini junit_duration_report = call
咱们有两种方式实现这个功能:
使用record_property fixture
:
为test_record_property
用例添加一个额外的test_id
:
# src/chapter-2/test_xml_report.py def test_record_property(record_property): record_property("test_id", 10010) assert 1
在报告中的表现为<property name="test_id" value="10010" />
:
<!-- src/chapter-2/report/test_record_property.xml --> <?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite errors="0" failures="0" hostname="NJ-LUYAO-T460" name="pytest_chinese_doc" skipped="0" tests="1" time="0.024" timestamp="2019-09-27T15:02:41.277369"> <testcase classname="test_xml_report" file="test_xml_report.py" line="22" name="test_record_property" time="0.002"> <properties> <property name="test_id" value="10010" /> </properties> </testcase> </testsuite> </testsuites>
解析一个自定义的标记@pytest.mark.test_id()
:
首先,修改pytest_collection_modifyitems
钩子方法,添加对test_id
标记的支持:
# src/chapter-2/conftest.py def pytest_collection_modifyitems(session, config, items): for item in items: for marker in item.iter_markers(name="test_id"): test_id = marker.args[0] item.user_properties.append(("test_id", test_id))
而后,修改测试用例:
# src/chapter-2/test_xml_report.py import pytest @pytest.mark.test_id(10010) def test_record_property1(): assert 1
在报告中的也表现为<property name="test_id" value="10010" />
:
<!-- src/chapter-2/report/test_record_property1.xml --> <?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite errors="0" failures="0" hostname="NJ-LUYAO-T460" name="pytest_chinese_doc" skipped="0" tests="1" time="0.029" timestamp="2019-09-27T15:16:05.309308"> <testcase classname="test_xml_report" file="test_xml_report.py" line="29" name="test_record_property1" time="0.001"> <properties> <property name="test_id" value="10010" /> </properties> </testcase> </testsuite> </testsuites>
注意:
这是咱们会接收到一个告警:
PytestUnknownMarkWarning: Unknown pytest.mark.test_id - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
这是由于咱们没有在
pytest
中注册test_id
标记,但不影响正常的执行;若是你想去除这个告警,只须要在
pytest.ini
的配置文件中注册这个标记:[pytest] markers = test_id: 为测试用例添加ID
注意:
变更后的报告可能不符合最新的
JUnitXML
的模式检查规则,致使在某些CI工具上可能会发生未知的错误;
能够经过record_xml_attribute fixture
为测试用例附加额外的属性,而不像record_property
为其添加子节点;
为测试用例添加一个test_id
属性,并修改原先的classname
属性:
# src/chapter-2/test_xml_report.py def test_record_property2(record_xml_attribute): record_xml_attribute('test_id', 10010) record_xml_attribute('classname', 'custom_classname') assert 1
在报告中的表现为<testcase classname="custom_classname" test_id="10010" ...
:
<!-- src/chapter-2/report/test_record_property2.xml --> <?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite errors="0" failures="0" hostname="NJ-LUYAO-T460" name="pytest_chinese_doc" skipped="0" tests="1" time="0.028" timestamp="2019-09-27T15:35:47.093494"> <testcase classname="custom_classname" file="test_xml_report.py" line="34" name="test_record_property2" test_id="10010" time="0.001"></testcase> </testsuite> </testsuites>
注意:
record_xml_attribute
目前是一个实验性的功能,将来可能被更强大的API所替代,但功能自己会被保留。变更后的报告可能不符合最新的
JUnitXML
的模式检查规则,致使在某些CI工具上可能会发生未知的错误;
4.5版本新增功能
能够经过自定义一个session
做用域级别的fixture
,为测试集添加子节点信息,而且会做用于全部的测试用例;
这个自定义的fixture
须要调用另一个record_testsuite_property fixture
:
record_testsuite_property
接收两个参数name
和value
以构成<property>
标签,其中,name
必须为字符串,value
会转换为字符串并进行XML转义;
# src/chapter-2/test_xml_report.py @pytest.fixture(scope="session") def log_global_env_facts(record_testsuite_property): record_testsuite_property("EXECUTOR", "luizyao") record_testsuite_property("LOCATION", "NJ") def test_record_property3(log_global_env_facts): assert 1
生成的测试报告表现为:在testsuite
节点中,多了一个properties
子节点,包含全部新增的属性节点,并且,它和全部的testcase
节点是平级的;
<!-- src/chapter-2/report/test_record_property3.xml --> <?xml version="1.0" encoding="utf-8"?> <testsuites> <testsuite errors="0" failures="0" hostname="NJ-LUYAO-T460" name="pytest_chinese_doc" skipped="0" tests="1" time="0.027" timestamp="2019-09-27T15:52:34.562238"> <properties> <property name="EXECUTOR" value="luizyao" /> <property name="LOCATION" value="NJ" /> </properties> <testcase classname="test_xml_report" file="test_xml_report.py" line="46" name="test_record_property3" time="0.002"> </testcase> </testsuite> </testsuites>
注意:
这样生成的XML文件是符合最新的
xunit
标准的,这点和record_property
、record_xml_attribute
正好相反;
不推荐使用,计划在
pytest 6.0
中删除这个功能
使用以下命令,能够在指定的path
中建立一个纯文本的测试报告:
pytest --resultlog=path
URL
连接 -- pastebin
服务目前,只实现了在http://bpaste.net上的展现功能;
为每个失败的测试用例建立一个URL
pytest --pastebin=failed
也能够经过添加-x
选项,只为第一个失败的测试用例建立一个URL;
为全部的测试用例建立一个URL
pytest --pastebin=all
你能够在命令行中使用-p
选项,来尽早的加载某一个插件:
pytest -p mypluginmodule
-p
选项接收一个name
参数,这个参数能够为:
myproject.plugins
,其必须是能够import
的。setuptools
中赋予的名字,例如:尽早的加载pytest-cov插件:bash pytest -p pytest_cov
你能够在命令行中使用-p
结合no:
,来去使能一个插件的加载,例如:
pytest -p no:doctest
python
代码中调用pytest
能够直接在代码中调用pytest
:
pytest.main()
这和你在命令行中执行pytest .
几乎是同样的,但其也有如下特色:
不会触发SystemExit
,而是返回exitcode:
# src/chapter-2/invoke_via_main.py import time def test_one(): time.sleep(10) if __name__ == '__main__': import pytest ret = pytest.main(['-q', __file__]) print("pytest.main() 返回 pytest.ExitCode.INTERRUPTED:", ret == pytest.ExitCode.INTERRUPTED)
用例中有等待10秒的操做,在这期间,打断执行(Ctr+C
),pytest.main()
返回的是INTERRUPTED
状态码;
λ pipenv run python src/chapter-2/invoke_via_main.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! D:\Personal Files\Projects\pytest-chinese-doc\src\chapter-2\invoke_via_main.py:26: KeyboardInterrupt (to show a full traceback on KeyboardInterrupt use --full-trace) no tests ran in 1.04s pytest.main() 返回 pytest.ExitCode.INTERRUPTED: True Aborted!
传递选项和参数:
pytest.main(["-x", "mytestdir"])
指定一个插件:
import pytest class MyPlugin: def pytest_sessionfinish(self): print("*** test run reporting finishing") pytest.main(["-qq"], plugins=[MyPlugin()])
注意:
调用
pytest.main()
会引入你的测试文件以及其引用的全部模块。因为python引入机制的缓存特性,当这些文件发生变化时,后续再调用pytest.main()
(在同一个程序执行过程当中)时,并不会响应这些文件的变化。基于这个缘由,咱们不推荐在同一个程序中屡次调用
pytest.main()
(例如:为了从新执行测试;若是你确实有这个需求,或许能够考虑pytest-repeat插件);