3.pytest命令行参数
3.1 pytest控制台信息详解
一般在运行pytest以后,会出现以下所示的控制台信息:html
C:\Users\Surpass\Documents\PycharmProjects\PytestStudy\Lesson01>pytest test_01.py ==================test session starts ======================= platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\Documents\PycharmProjects\PytestStudy\Lesson01 collected 2 items test_01.py .F [100%] ===================FAILURES ================================= __________________ test_add_02 ______________________________ def test_add_02(): > assert Add(3,4)==6 E assert 7 == 6 E + where 7 = Add(3, 4) test_01.py:12: AssertionError ================ short test summary info ==================== FAILED test_01.py::test_add_02 - assert 7 == 6 =============== 1 failed, 1 passed in 0.56s ==================
- ===== test session starts =====
测试会话分隔符,表明启动了一个新的调用python
- platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
当前运行的平台,Python、pytest、pytest包的版本等信息微信
- rootdir
当前起始目录,pytest搜索测试代码时使用的目录session
- collected 2 items
在搜索的目录中找到的测试函数个数框架
- test_01.py .F
test_01.py表明测试文件函数
- FAILED
未经过的测试函数的失败缘由单元测试
- 1 failed, 1 passed in 0.14s
统计测试函数经过和未经过的数量及运行时间学习
3.2 Pytest运行结果类型
在Pytest中,测试函数可能会返回多种结果,不像传统的单元测试框架,仅显示经过和失败。如下是可能返回的结果:测试
- PASSED(.)
测试经过。预期成功,实际成功字体
- F(FAILED)
表示测试未经过,也多是XPASS状态与strict选项形成的冲突。预期成功,实际失败
- SKIPPED(s)
测试函数未执行,即该运行测试时跳过该测试函数,经常使用测试标记@pytest.mark.skip()或@pytest.mark.skipif()指定跳过测试的条件
- xfail(x)
指望测试失败,并且确实失败。经常使用测试标记@pytest.mark.xfail()来指定认为会失败的测试函数。预期失败,实际失败
- XPASS(X)
指望测试失败,但实际结果倒是运行经过,即不符合预期。预期失败,实际成功
- ERROR(E)
运行测试函数时,出现异常或错误,可能由fixture引发
3.3 Pytest命令行参数
不少时候,运行单元测试框架并不须要界面,更多的时候采用命令行的形式执行,pytest也提供不少的命令行参数,获取命令行参数帮助以下所示:
pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...] positional arguments: file_or_dir general: -x, --exitfirst exit instantly on first error or failed test.
下面来学习一些经常使用的命令行参数
3.3.1 --collect-only
该参数仅收集测试函数,但并不执行运行操做。以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --collect-only ============================= test session starts ================ platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 3 items <Module test_01.py> <Function test_add_01> <Function test_add_02> <Module test_02.py> <Function test_requestBaidu>
3.3.2 -k Expression
-k选项容许使用表达式来指按期望运行的测试函数。经常使用于筛选要进行运行的测试函数。在如下示例中,仅运行当前及其子目录中测试函数名含request或temp的测试函数,源代码以下所示:
- test_01.py
import pytest # First of sample function def Add(x:int,y:int)->int: return x+y # First of test function def test_add_01(): assert Add(2,3)==5 def test_add_02(): assert Add(3,4)==6 def test_request_01(): assert Add(4,3)==7 def test_temp_01(): assert Add(8,3)==11
- test_02.py
import requests import pytest url = "http://www.baidu.com" port = 80 def test_requestBaidu(): testUrl=f"{url}:{port}" r=requests.get(url=testUrl,timeout=10) content=r.content.decode("utf8") print(content) assert "bai" in content
如下先验证筛选的结果是否正确: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -k "request or temp" --collect-only ============================== test session starts ======================================= platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 5 items / 2 deselected / 3 selected <Module test_01.py> <Function test_request_01> <Function test_temp_01> <Module test_02.py> <Function test_requestBaidu> =========================== 2 deselected in 2.36s ================================= 实际运行结果: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -k "request or temp" -v ======================== test session starts ====================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe cachedir: .pytest_cache rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 5 items / 2 deselected / 3 selected test_01.py::test_request_01 PASSED [ 33%] test_01.py::test_temp_01 PASSED [ 66%] test_02.py::test_requestBaidu PASSED [100%] ====================== 3 passed, 2 deselected in 0.52s ==============================
3.3.3 -m MarkExpr
该选项经常使用于标记测试并分组,方便快速运行选中的测试函数,具体的标记能够自定义,可使用装饰器@pytest.mark。示例以下:
- test_01.py
import pytest # First of sample function def Add(x:int,y:int)->int: return x+y @pytest.mark.TestMark # First of test function def test_add_01(): assert Add(2,3)==5 def test_add_02(): assert Add(3,4)==6 @pytest.mark.RunThisTestFunc def test_request_01(): assert Add(4,3)==7 def test_temp_01(): assert Add(8,3)==11
- test_01.py
import requests import pytest url = "http://www.baidu.com" port = 80 @pytest.mark.RunThisTestFunc def test_requestBaidu(): testUrl=f"{url}:{port}" r=requests.get(url=testUrl,timeout=10) content=r.content.decode("utf8") print(content) assert "bai" in content
运行结果以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -m "RunThisTestFunc or not TestMark" --collect-only =========================== test session starts ==================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 5 items / 1 deselected / 4 selected <Module test_01.py> <Function test_add_02> <Function test_request_01> <Function test_temp_01> <Module test_02.py> <Function test_requestBaidu> C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -m "RunThisTestFunc or not TestMark" -v ========================== test session starts ======================================= platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe cachedir: .pytest_cache rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 5 items / 1 deselected / 4 selected test_01.py::test_add_02 FAILED [ 25%] test_01.py::test_request_01 PASSED [ 50%] test_01.py::test_temp_01 PASSED [ 75%] test_02.py::test_requestBaidu PASSED [100%]
3.3.4 -x, --exitfirst
正常状况下,pytest会运行每个搜索到的测试函数,若是某个测试函数断言失败或触发了异常,则该测试函数的运行就中止了,此时pytest会将其标记为失败,而后继续运行后续的测试函数,这也是咱们指望的运行模式。那若是咱们但愿在某一个测试函数在运行失败,则中断再也不运行后续的测试函数,此时就可使用-x选项。
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -x ========================== test session starts ========================================= platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items test_01.py .F =============================== FAILURES ========================================== _____________________________________________________ test_add_02 _____________________________________________________ def test_add_02(): > assert Add(3,4)==6 E assert 7 == 6 E + where 7 = Add(3, 4) test_01.py:13: AssertionError ==================== 1 failed, 1 passed, 3 warnings in 0.32s =========================
从输出的信息能够看出,总共有6个测试函数,而在运行第2个测试函数,断言失败,则中止整个运行过程,所以,仅运行了2个测试函数,一个测试经过,一个测试失败。
3.3.5 --maxfail=num
-x选项的特色是一遇到失败,就会直接所有中止。而若是想要pytest在失败n次后再中止,则可经过选项-maxfail指定容许失败的次数。
import pytest # First of sample function def Add(x:int,y:int)->int: return x+y @pytest.mark.TestMark # First of test function def test_add_01(): assert Add(2,3)==5 def test_add_02(): assert Add(3,4)==6 @pytest.mark.RunThisTestFunc def test_request_01(): assert Add(4,3)==7 def test_temp_01(): assert Add(8,3)==12
运行结果以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --maxfail=2 =============================== test session starts ===================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items test_01.py .F.F ================================= FAILURES ========================================== _________________________________ test_add_02 __________________________________________ def test_add_02(): > assert Add(3,4)==6 E assert 7 == 6 E + where 7 = Add(3, 4) test_01.py:13: AssertionError ________________________________ test_temp_01 _________________________________________ def test_temp_01(): > assert Add(8,3)==12 E assert 11 == 12 E + where 11 = Add(8, 3) test_01.py:20: AssertionError ====================== 2 failed, 2 passed, 3 warnings in 0.35s =========================
若是--maxfail=1,则功能与-x同样
3.3.6 --lf --last-failed
当一个或多个测试函数运行失败后,咱们经常但愿可以定位到最后一个运行失败的测试函数,而且但愿能从新运行,这时则可使用--lf选项
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --lf ============================== test session starts ==================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 4 items / 2 deselected / 2 selected run-last-failure: rerun previous 2 failures (skipped 3 files) test_01.py FF [100%] =================================== FAILURES ========================================== ____________________________ test_add_02 ________________________________________ def test_add_02(): > assert Add(3,4)==6 E assert 7 == 6 E + where 7 = Add(3, 4) test_01.py:13: AssertionError _____________________________________test_temp_01 _________________________________________ def test_temp_01(): > assert Add(8,3)==12 E assert 11 == 12 E + where 11 = Add(8, 3) test_01.py:20: AssertionError ==================== 2 failed, 2 deselected, 2 warnings in 0.13s =============================
3.3.7 --ff --failed-first
--ff选项与--lf选项功能基本相同,不一样之处以下所示:
--ff 先运行上次失败的测试函数再运行完剩余的测试函数
--lf 仅运行上次失败的测试函数
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff =============================== test session starts ======================================= platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items run-last-failure: rerun previous 2 failures first test_01.py FF.. [ 66%] test_02.py . [ 83%] test_03.py . [100%] ================================= FAILURES ====================================== ______________________________________test_add_02 ____________________________________________ def test_add_02(): > assert Add(3,4)==6 E assert 7 == 6 E + where 7 = Add(3, 4) test_01.py:13: AssertionError _______________________________________ test_temp_01 _________________________________________ def test_temp_01(): > assert Add(8,3)==12 E assert 11 == 12 E + where 11 = Add(8, 3) test_01.py:20: AssertionError ======================== 2 failed, 4 passed, 3 warnings in 1.88s ======================
3.3.8 -v --verbose
使用-v/--verbose,经常使用于输出详细的运行信息。最主要的区别是每一个文件中的每一个测试函数都占用一行(以前是每个文件占用一行),测试的结果和名字都会显示出来,而再也不在是一个点或字符,以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff --tb=no -v ======================== test session starts ===================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe cachedir: .pytest_cache rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items run-last-failure: rerun previous 2 failures first test_01.py::test_add_02 FAILED [ 16%] test_01.py::test_temp_01 FAILED [ 33%] test_01.py::test_add_01 PASSED [ 50%] test_01.py::test_request_01 PASSED [ 66%] test_02.py::test_requestBaidu PASSED [ 83%] test_03.py::test_request PASSED [100%] ====================== 2 failed, 4 passed, 3 warnings in 0.93s =============================
若是运行是彩色终端,则FAILED为红色字体,PASS为绿色字体。
3.3.9 -q --quiet
该选项与-v功能相反,该选项会输出简化信息,只保留核心信息。以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --ff --tb=line -q FF.... [100%] ======================================== FAILURES ==================================== C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6 C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12 2 failed, 4 passed, 3 warnings in 1.34s
3.3.10 --tb=style
--tb参数主要决定捕捉到失败后的输出信息的显示方式。其可选值为 auto/long/short/line/native/no,其经常使用模式解释以下:
- auto模式:默认值,若是有多个测试函数失败,仅打印第一个和最后一个用例的回溯信息
- long模式:输出的信息最详细
- short模式:仅输出assert的一行及系统断定内容,不显示上下文
- line模式:只使用一行输出显示全部的错误信息
- native模式:只输出Python标准库的回溯信息,不显示额外信息
- no模式:直接屏蔽所有回溯信息
以上模式经常使用模式为:short/line/no
某个测试函数运行失败后,pytest会列举出失败信息,包括失败出如今哪一行,什么缘由致使的失败,该过程称之为信息回溯
示例以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest -v --tb=line ============================ test session starts ================================ platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe cachedir: .pytest_cache rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items test_01.py::test_add_01 PASSED [ 16%] test_01.py::test_add_02 FAILED [ 33%] test_01.py::test_request_01 PASSED [ 50%] test_01.py::test_temp_01 FAILED [ 66%] test_02.py::test_requestBaidu PASSED [ 83%] test_03.py::test_request PASSED [100%] ==================================== FAILURES ========================================== C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6 C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12 ========================= 2 failed, 4 passed, 3 warnings in 2.10s ===========================
3.3.11 --durations=N
--durations=N 该选项不关注测试具体的运行过程,只统计测试过程当中哪向个阶段最慢,所以使用该选项能够加快测试分节奏,显示运行最慢的N个阶段,耗时越长越靠前,若是--durations=0,则将全部阶段的耗时从长到短排序后显示。示例以下所示:
C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01>pytest --durations=3 --tb=line -v ============================test session starts ========================================== platform win32 -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- d:\program files\python\python.exe cachedir: .pytest_cache rootdir: C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01 collected 6 items test_01.py::test_add_01 PASSED [ 16%] test_01.py::test_add_02 FAILED [ 33%] test_01.py::test_request_01 PASSED [ 50%] test_01.py::test_temp_01 FAILED [ 66%] test_02.py::test_requestBaidu PASSED [ 83%] test_03.py::test_request PASSED [100%] =============================== FAILURES ========================================= C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:13: assert 7 == 6 C:\Users\Surpass\PycharmProjects\PytestStudy\Lesson01\test_01.py:20: assert 11 == 12 =============================== warnings summary ====================================== ================================ slowest 3 test durations ================================= 0.50s call test_03.py::test_request 0.06s call test_02.py::test_requestBaidu (0.00 durations hidden. Use -vv to show these durations.) ======================= 2 failed, 4 passed, 3 warnings in 0.93s ===========================
3.3.12 -h --help
以上仅仅是经常使用的一些选项,更多选项可经过-h或--help来进行查看,经过该选项不但能展现原生pytest的用法,还能展现新添加插件的选项和用法。
原文地址:https://www.cnblogs.com/surpassme/p/13252310.html
本文同步在微信订阅号上发布,如各位小伙伴们喜欢个人文章,也能够关注个人微信订阅号:woaitest,或扫描下面的二维码添加关注: