Pytest单元测试系列[v1.0.0][pytest插件常用技巧]
使用pytest-xdist并发执行测试
pytest-xdist:Run Tests in Parallel
[https://pypi.python.org/pypi/pytest-xdist]
在自动化测试中有些资源只能同时被一个测试用例访问,如果不需要同时使用同一个资源,那么测试用例便可以并行执行
执行命令pip install pytest-xdist安装插件
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-xdist
Collecting pytest-xdistDownloading https://files.pythonhosted.org/packages/9f/cc/371b2e6dfbf4e8df07b04e310dd6ea0b3f367e257d1e6cb516b25bc4af1b/pytest_xdist-1.29.0-py2.py3-none-any.whl
Collecting pytest-forked (from pytest-xdist)Downloading https://files.pythonhosted.org/packages/3f/55/ef92c340e723495dbee91d749903d2b7950b49c501943296257246d7d880/pytest_forked-1.0.2-py2.py3-none-any.whl
Requirement already satisfied: six in c:\python37\lib\site-packages (from pytest-xdist) (1.12.0)
Requirement already satisfied: pytest>=4.4.0 in c:\python37\lib\site-packages (from pytest-xdist) (4.5.0)
Collecting execnet>=1.1 (from pytest-xdist)Downloading https://files.pythonhosted.org/packages/77/1a/f69e1f73bc36f55d3273afd1c52936def71ac67d9c5215be3a4ca3a45577/execnet-1.6.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=4.4.0->pytest-xdist) (41.0.1)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.8.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.4.1)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (19.1.0)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.11.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.3.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.1.7)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (7.0.0)
Collecting apipkg>=1.4 (from execnet>=1.1->pytest-xdist)Downloading https://files.pythonhosted.org/packages/67/08/4815a09603fc800209431bec5b8bd2acf2f95abdfb558a44a42507fb94da/apipkg-1.5-py2.py3-none-any.whl
Installing collected packages: pytest-forked, apipkg, execnet, pytest-xdist
Successfully installed apipkg-1.5 execnet-1.6.0 pytest-forked-1.0.2 pytest-xdist-1.29.0
使用pytest-xdist执行测试
E:\Programs\Python\Python_Pytest\TestScripts>pytest -n auto
================================ test session starts ================================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, repeat-0.8.0, forked-1.0.2, allure-pytest-2.6.3
gw0 [17] / gw1 [17] / gw2 [17] / gw3 [17]
...........FF..F. [100%]
=========================== FAILURES ================================================
________________________________________ test_true ____________________________________________
[gw2] win32 -- Python 3.7.3 c:\python37\python.exedef test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionError
__________________________________________ test_add4 ___________________________________________
[gw3] win32 -- Python 3.7.3 c:\python37\python.exedef test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionError
______________________________ test_not_equal _____________________________________________
[gw1] win32 -- Python 3.7.3 c:\python37\python.exedef test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full difftest_one.py:9: AssertionError
============================== 3 failed, 14 passed, 4 warnings in 2.20 seconds ===============================
参数说明
-n auto 自动侦测系统里的CPU数目
-n numprocesses 指定运行测试的处理器进程数
使用pytest-repeat重复执行用例
pytest-repeat:Run Tests More Than Once
运行命令pip install pytest-repeat[https://pypi.python.org/pypi/pytest-repeat]
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-repeat
Collecting pytest-repeatDownloading https://files.pythonhosted.org/packages/2e/de/c1d69002db74a99b3df0463e95066c03d82d9d2a53be738c140207134e0f/pytest_repeat-0.8.0-py2.py3-none-any.whl
Requirement already satisfied: pytest>=3.6 in c:\python37\lib\site-packages (from pytest-repeat) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (19.1.0)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.11.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.1.7)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (7.0.0)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.8.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.4.1)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=3.6->pytest-repeat) (41.0.1)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.12.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.3.0)
Installing collected packages: pytest-repeat
Successfully installed pytest-repeat-0.8.0
执行测试,运行命令pytest --count=2 -v
E:\Programs\Python\Python_Pytest\TestScripts>pytest --count=2 -v
======================== test session starts==============================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: repeat-0.8.0, allure-pytest-2.6.3
collected 34 items test_asserts.py::test_add[1-2] PASSED [ 2%]
test_asserts.py::test_add[2-2] PASSED [ 5%]
test_asserts.py::test_add2[1-2] PASSED [ 8%]
test_asserts.py::test_add2[2-2] PASSED [ 11%]
test_asserts.py::test_add3[1-2] PASSED [ 14%]
test_asserts.py::test_add3[2-2] PASSED [ 17%]
test_asserts.py::test_add4[1-2] FAILED [ 20%]
test_asserts.py::test_add4[2-2] FAILED [ 23%]
test_asserts.py::test_in[1-2] PASSED [ 26%]
test_asserts.py::test_in[2-2] PASSED [ 29%]
test_asserts.py::test_not_in[1-2] PASSED [ 32%]
test_asserts.py::test_not_in[2-2] PASSED [ 35%]
test_asserts.py::test_true[1-2] FAILED [ 38%]
test_asserts.py::test_true[2-2] FAILED [ 41%]
test_fixture1.py::test_numbers_3_4[1-2] PASSED [ 44%]
test_fixture1.py::test_numbers_3_4[2-2] PASSED [ 47%]
test_fixture1.py::test_strings_a_3[1-2] PASSED [ 50%]
test_fixture1.py::test_strings_a_3[2-2] PASSED [ 52%]
test_fixture2.py::TestUM::test_numbers_5_6[1-2] PASSED [ 55%]
test_fixture2.py::TestUM::test_numbers_5_6[2-2] PASSED [ 58%]
test_fixture2.py::TestUM::test_strings_b_2[1-2] PASSED [ 61%]
test_fixture2.py::TestUM::test_strings_b_2[2-2] PASSED [ 64%]
test_one.py::test_equal[1-2] PASSED [ 67%]
test_one.py::test_equal[2-2] PASSED [ 70%]
test_one.py::test_not_equal[1-2] FAILED [ 73%]
test_one.py::test_not_equal[2-2] FAILED [ 76%]
test_two.py::test_default[1-2] PASSED [ 79%]
test_two.py::test_default[2-2] PASSED [ 82%]
test_two.py::test_member_access[1-2] PASSED [ 85%]
test_two.py::test_member_access[2-2] PASSED [ 88%]
test_two.py::test_asdict[1-2] PASSED [ 91%]
test_two.py::test_asdict[2-2] PASSED [ 94%]
test_two.py::test_replace[1-2] PASSED [ 97%]
test_two.py::test_replace[2-2] PASSED [100%]============================================ FAILURES ====================================
________________________________ test_add4[1-2] _______________________________________def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionError
_________________________ test_add4[2-2] ______________________________________________def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionError
________________________ test_true[1-2] __________________________________________def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionError
____________________________ test_true[2-2] ______________________________________________def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionError
__________________________ test_not_equal[1-2] ____________________________________________def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (1, 2, 3)
E ? ^ ^
E + (3, 2, 1)
E ? ^ ^test_one.py:9: AssertionError
_________________________________ test_not_equal[2-2] ____________________________________def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (1, 2, 3)
E ? ^ ^
E + (3, 2, 1)
E ? ^ ^test_one.py:9: AssertionError
=================== 6 failed, 28 passed, 1 warnings in 0.77 seconds =======================
可与重复一个测试子集,后者某一个测试,甚至可以让他在晚上重复执行N次,同时可以让他遇到错误就停止
使用pytest-instafail查看详细堆栈信息
当测试执行遇到失败或错误时能及时看到详细的堆栈信息
pytest-instafail:See Details of Failures and Errors
[https://pypi.python.org/pypi/pytest-instafail]
执行命令pip install pytest-instafail安装插件
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-instafail
Collecting pytest-instafailDownloading https://files.pythonhosted.org/packages/fa/16/473621ad68cc2a1cb2888478e66db5080a06adf695470c8dd4ec669c25d5/pytest-instafail-0.4.1.tar.gz
Requirement already satisfied: pytest>=2.9 in c:\python37\lib\site-packages (from pytest-instafail) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (19.1.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (7.0.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.12.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.3.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=2.9->pytest-instafail) (41.0.1)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.11.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.4.1)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.1.7)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.8.0)
Building wheels for collected packages: pytest-instafailBuilding wheel for pytest-instafail (setup.py) ... doneStored in directory: C:\Users\davieyang\AppData\Local\pip\Cache\wheels\16\cb\de\3a1d2f5c992fedf9a86b8eead949a606a6c953228ac1fe0655
Successfully built pytest-instafail
Installing collected packages: pytest-instafail
Successfully installed pytest-instafail-0.4.1
通常情况下,pytest执行完毕后,会显示错误和失败用例的堆栈信息,如果测试用例比较多,运行时间太长,很可能我们希望不是到最才看到堆栈回溯信息,使用此插件便可以测试执行失败立马显示异常信息
执行测试
E:\Programs\Python\Python_Pytest\TestScripts>pytest --tb=line --instafail
========================= test session starts ========================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, allure-pytest-2.6.3
collected 17 items test_asserts.py ...FE:\Programs\Python\Python_Pytest\TestScripts\test_asserts.py:34: assert 39 >= 50test_asserts.py ..FE:\Programs\Python\Python_Pytest\TestScripts\test_asserts.py:65: assert not Truetest_fixture1.py .. [ 52%]
test_fixture2.py .. [ 64%]
test_one.py .FE:\Programs\Python\Python_Pytest\TestScripts\test_one.py:9: assert (1, 2, 3) == (3, 2, 1)test_two.py .... [100%]
=================== 3 failed, 14 passed, 1 warnings in 0.32 seconds =======================
使用pytest-timeout设置执行测试的时限
pytest-timeout:Put Time Limits on Your Tests
[https://pypi.python.org/pypi/pytest-time]
运行命令pip install pytest-timeout
,安装插件
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-timeout
Collecting pytest-timeoutDownloading https://files.pythonhosted.org/packages/58/92/f60ea2e27074d6f97c8aaf21e34d1f838eb623e4b8070680846c65318a10/pytest_timeout-1.3.3-py2.py3-none-any.whl
Requirement already satisfied: pytest>=3.6.0 in c:\python37\lib\site-packages (from pytest-timeout) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (19.1.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.3.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.12.0)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.8.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (7.0.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.4.1)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.1.7)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.11.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=3.6.0->pytest-timeout) (41.0.1)
Installing collected packages: pytest-timeout
Successfully installed pytest-timeout-1.3.3
执行命令pytest --timeout=0.1
E:\Programs\Python\Python_Pytest\TestScripts>pytest --timeout=0.1
======================== test session starts =============================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, forked-1.0.2, allure-pytest-2.6.3
timeout: 0.1s
timeout method: thread
timeout func_only: False
collected 17 items test_asserts.py ...F..F [ 41%]
test_fixture1.py .. [ 52%]
test_fixture2.py .. [ 64%]
test_one.py .F [ 76%]
test_two.py .... [100%]================================== FAILURES ===============================================
_________________________________ test_add4 ______________________________________________def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionError
______________________________ test_true ______________________________________________def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionError
________________________________ test_not_equal _________________________________________def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full difftest_one.py:9: AssertionError==================== 3 failed, 14 passed, 1 warnings in 0.32 seconds ======================
使用Tox在多环境下的用例执行
tox 测试多种配置
tox是个命令行工具,它允许测试在多种环境下运行,不仅仅是不同的python版本,可以以用它来测试不同的依赖配置和不同的操作系统配置,其工作方式是通过setup.py文件为待测程序创建源码安装包,他会查看tox.ini中的所有环境设置,并针对每个环境设置执行如下操作
安装tox
C:\Users\davieyang>pip install tox
Collecting toxUsing cached https://files.pythonhosted.org/packages/a7/0c/ed234b83d2c4fcef1cfccf97371183d51dafae62e64334de34d0a6333114/tox-3.14.0-py2.py3-none-any.whl
Collecting importlib-metadata<1,>=0.12; python_version < "3.8" (from tox)Downloading https://files.pythonhosted.org/packages/f6/d2/40b3fa882147719744e6aa50ac39cf7a22a913cbcba86a0371176c425a3b/importlib_metadata-0.23-py2.py3-none-any.whl
Requirement already satisfied: py<2,>=1.4.17 in c:\python37\lib\site-packages (from tox) (1.8.0)
Requirement already satisfied: six<2,>=1.0.0 in c:\python37\lib\site-packages (from tox) (1.12.0)
Collecting toml>=0.9.4 (from tox)Using cached https://files.pythonhosted.org/packages/a2/12/ced7105d2de62fa7c8fb5fce92cc4ce66b57c95fb875e9318dba7f8c5db0/toml-0.10.0-py2.py3-none-any.whl
Requirement already satisfied: packaging>=14 in c:\python37\lib\site-packages (from tox) (19.0)
Collecting filelock<4,>=3.0.0 (from tox)Downloading https://files.pythonhosted.org/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl
Collecting virtualenv>=14.0.0 (from tox)Downloading https://files.pythonhosted.org/packages/8b/12/8d4f45b8962b03ac9efefe5ed5053f6b29334d83e438b4fe379d21c0cb8e/virtualenv-16.7.5-py2.py3-none-any.whl (3.3MB)100% |████████████████████████████████| 3.3MB 9.9kB/s
Collecting pluggy<1,>=0.12.0 (from tox)Downloading https://files.pythonhosted.org/packages/92/c7/48439f7d5fd6bddb4c04b850bb862b42e3e2b98570040dfaf68aedd8114b/pluggy-0.13.0-py2.py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata<1,>=0.12; python_version < "3.8"->tox)Downloading https://files.pythonhosted.org/packages/74/3d/1ee25a26411ba0401b43c6376d2316a71addcc72ef8690b101b4ea56d76a/zipp-0.6.0-py2.py3-none-any.whl
Requirement already satisfied: pyparsing>=2.0.2 in c:\python37\lib\site-packages (from packaging>=14->tox) (2.4.0)
Requirement already satisfied: more-itertools in c:\python37\lib\site-packages (from zipp>=0.5->importlib-metadata<1,>=0.12; python_version < "3.8"->tox) (7.0.0)
Installing collected packages: zipp, importlib-metadata, toml, filelock, virtualenv, pluggy, toxFound existing installation: pluggy 0.11.0Uninstalling pluggy-0.11.0:Successfully uninstalled pluggy-0.11.0
Successfully installed filelock-3.0.12 importlib-metadata-0.23 pluggy-0.13.0 toml-0.10.0 tox-3.14.0 virtualenv-16.7.5 zipp-0.6.0
配置tox.ini
- 在tox目录下创建一个虚拟环境
- 使用pip安装依赖包
- 使用pip在步骤1的虚拟环境中安装自己的程序包
- 运行测试用例
- 所有环境都测试完成后,tox生成一个汇总的测试结果
在项目文件最顶层添加tox.ini文件(与setup.py放在一个目录里),然后在tox.ini文件里写入如下内容
;---
; Excerpted from "Python Testing with pytest",
; published by The Pragmatic Bookshelf.
; Copyrights apply to this code. It may not be used to create training material,
; courses, books, articles, and the like. Contact us if you are in doubt.
; We make no guarantees that this code is fit for any purpose.
; Visit http://www.pragmaticprogrammer.com/titles/bopytest for more book information.
;---
# tox.ini , put in same dir as setup.py[tox]
envlist = py27,py37[testenv]
deps=pytest
commands=pytest[pytest]
addopts = -rsxX -l --tb=short --strict
markers = smoke: Run the smoke test test functionsget: Run the test functions that test tasks.get()
选项 | 描述 |
---|---|
envlist=py27,py36 | 表示使用python2.7和python3.6来运行测试用例 |
deps=pytest | 表示让tox确保pytest已经安装,如果有多个依赖还可以继续以相同方式罗列,并且还可以指定版本 |
commands=pytest | 告诉tox在每个环境里运行pytest |
执行tox
然后在tox.ini所在路径下执行tox命令即可,它会使用指定的两个环境执行该路径下tests文件夹内的所有测试用例。
(venv) E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2>tox
GLOB sdist-make: E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2\setup.py
py37 create: E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2\.tox\py37
py37 installdeps: pytest
......
----------------------------------Summary----------------------------------------------------------------------------------
py27: commands succeeded
py36: commands succeeded
congratulations :)
使用pytest-sugar让测试结果更有趣
pytest-sugar: instafail+Colors+Progress Bar
使用命令pip install pytest-sugar安装插件
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-sugar
Collecting pytest-sugarDownloading https://files.pythonhosted.org/packages/da/3b/f1e3c8830860c1df8f0e0f6713932475141210cfa021e362ca2774d2bf02/pytest_sugar-0.9.2-py2.py3-none-any.whl
Requirement already satisfied: pytest>=2.9 in c:\python37\lib\site-packages (from pytest-sugar) (4.5.0)
Collecting packaging>=14.1 (from pytest-sugar)Downloading https://files.pythonhosted.org/packages/91/32/58bc30e646e55eab8b21abf89e353f59c0cc02c417e42929f4a9546e1b1d/packaging-19.0-py2.py3-none-any.whl
Collecting termcolor>=1.1.0 (from pytest-sugar)Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.3.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.1.7)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (19.1.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.12.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.4.1)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.8.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=2.9->pytest-sugar) (41.0.1)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.11.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (7.0.0)
Collecting pyparsing>=2.0.2 (from packaging>=14.1->pytest-sugar)Downloading https://files.pythonhosted.org/packages/dd/d9/3ec19e966301a6e25769976999bd7bbe552016f0d32b577dc9d63d2e0c49/pyparsing-2.4.0-py2.py3-none-any.whl (62kB)100% |████████████████████████████████| 71kB 19kB/s
Building wheels for collected packages: termcolorBuilding wheel for termcolor (setup.py) ... doneStored in directory: C:\Users\davieyang\AppData\Local\pip\Cache\wheels\7c\06\54\bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
Successfully built termcolor
Installing collected packages: pyparsing, packaging, termcolor, pytest-sugar
Successfully installed packaging-19.0 pyparsing-2.4.0 pytest-sugar-0.9.2 termcolor-1.1.0
执行测试
E:\Programs\Python\Python_Pytest\TestScripts>pytest
Test session starts (platform: win32, Python 3.7.3, pytest 4.5.0, pytest-sugar 0.9.2)
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, sugar-0.9.2, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, allure-pytest-2.6.3
collecting ... test_asserts.py ✓ 6% ▋test_asserts.py ✓✓ 12% █▎test_asserts.py ✓✓✓ 18% █▊――――――――――――――――――――――――――――――――――――――――――test_add4――――――――――――――――――――――――――――――――――――――――def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)test_asserts.py:34: AssertionErrortest_asserts.py ⨯ 24% ██▍test_asserts.py ⨯✓ 29% ██▉test_asserts.py ⨯✓✓ 35% ███▌――――――――――――――――――――――――――――――――――――――― test_true――――――――――――――――――――――――――――――――――――――――――def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)test_asserts.py:65: AssertionErrortest_asserts.py ⨯ 41% ████▎test_fixture1.py ✓ 47% ████test_fixture1.py ✓✓ 53% ████
█▍ test_fixture2.py ✓ 59% ████test_fixture2.py ✓✓ 65% ████
██▌ test_one.py ✓ 71% ████
███▏ ―――――――――――――――――――――――――――――――――――― test_not_equal ―――――――――――――――――――――――――――――――――――――――def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full difftest_one.py:9: AssertionErrortest_one.py ⨯ 76% █████
██▋ test_two.py ✓ 82% ████test_two.py ✓✓ 88% ████test_two.py ✓✓✓ 94% ███test_two.py ✓✓✓✓ 100% ███
███████
==================================== warnings summary ===================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo? You can register custom mar
ks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.htmlPytestUnknownMarkWarning,-- Docs: https://docs.pytest.org/en/latest/warnings.htmlResults (0.47s):14 passed3 failed- test_asserts.py:33 test_add4- test_asserts.py:64 test_true- test_one.py:8 test_not_equal
使用PDB在测试失败时开启调试模式
PDB试失败的测试用例
--pdb
是Python标准库里的调试模块,在pytest里,可以使用–pdb选项在测试失败是开启调试的交互模式
(venv) E:\Programs\Python\Python_Pytest\TestScripts>pytest --pdb -v -x --lf
========================== test session starts =====================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts, inifile: pytest.ini
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, nice-0.1.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 119 items
run-last-failure: no previously failed tests, not deselecting items.test_api_exceptions.py::TestAdd::test_missing_summary PASSED [ 0%]
test_api_exceptions.py::TestAdd::test_done_not_bool FAILED [ 1%]
>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>self = <TestScripts.test_api_exceptions.TestAdd object at 0x0000000CEEA430F0>def test_done_not_bool(self):"""Should raise an exception if done is not a bool."""with pytest.raises(ValueError):
> tasks.add(Task(summary='summary', done='True'))
E Failed: DID NOT RAISE <class 'ValueError'>test_api_exceptions.py:19: Failed
>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> e:\programs\python\python_pytest\testscripts\test_api_exceptions.py(19)test_done_not_bool()
-> tasks.add(Task(summary='summary', done='True'))
(Pdb)
加上了--pdb
参数的执行结果,我们看到最后进入了(Pdb)的交互模式,(Pdb)提示符出现了,表明可以使用pdb的交互调试功能
参数 | 介绍 |
---|---|
p/print expr | 输出expr的值 |
pp expr | 美化输出expr的值 |
l/list | 列出错误并显示错误之前和之后的5行代码 |
l/list begin,end | 列出错误,并通过行号指定需要显示的代码区域 |
a/args | 打印当前函数的参数列表(断点发生在帮助函数中会很实用) |
u/up | 移动到堆栈的上一层 |
d/down | 移动到堆栈的下一层 |
q/quit | 退出当前调试会话 |
更多的pdb使用方法可参考PDB详细的使用方法 |
相关文章:
Pytest单元测试系列[v1.0.0][pytest插件常用技巧]
使用pytest-xdist并发执行测试 pytest-xdist:Run Tests in Parallel [https://pypi.python.org/pypi/pytest-xdist] 在自动化测试中有些资源只能同时被一个测试用例访问,如果不需要同时使用同一个资源,那么测试用例便可以并行执行 执行命令…...
嵌入式培训机构四个月实训课程笔记(完整版)-Linux系统编程第五天-Linux消息共享内存练习题(物联技术666)
更多配套资料CSDN地址:点赞+关注,功德无量。更多配套资料,欢迎私信。 物联技术666_嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记-CSDN博客物联技术666擅长嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记,等方面的知识,物联技术666关注机器学习,arm开发,物联网,嵌入式硬件,单片机…...
04set注入专题/简单类型/数组/List/Set/Map/空字符串/null/特殊符号
1.1注入外部Bean 在之前使用的案例就是注入外部Bean的方式。 <!-- class属性声明要管理哪个类中的对象 property标签的name是提示set方法名ref标签指明注入的bean的id--><bean id"userServiceBean" class"com.powernode.spring6.service.UserService…...
Linux引导和服务管理
目录 一.Linux引导: 1、Linux开机启动的完整过程: 2、bios的作用: 3、boot: 4.mbr: 5、grub: 6、加载内核文件: 7、启动进程: 8、centos6与centos7的区别: 9、完整的过程 …...
HarmonyOS 应用开发学习笔记 ets自定义组件及其引用 @Component自定义组件
Component注解的作用是用来构建自定义组件 Component组件官方文档 自定义组件具有以下特点: 可组合:允许开发者组合使用系统组件、及其属性和方法。 可重用:自定义组件可以被其他组件重用,并作为不同的实例在不同的父组件或容器…...
在做题中学习(43):长度最小的子数组
LCR 008. 长度最小的子数组 - 力扣(LeetCode) 解法:同向双指针-------滑动窗口算法 解释:本是暴力枚举做法,因为全部是正整数,就可以利用单调性和双指针解决问题来节省时间 思路: 如上面图&am…...
如何将 element-ui 中的 el-select 默认展开
<el-form-item label"藕粉桂花糖糕" prop"state" required><el-selectref"mySelect"v-model"form.state"style"width: 280px"placeholder"请选择"><el-option label"藕粉" :value"…...
Typora基本用法
文章目录 一、标题标题快捷键 二、段落1.换行2.分割线 三、文字显示1.字体2.上下标 四、列表1.无序列表2.有序列表3.任务列表 五、区块显示六、代码显示1.行内代码2.代码块 七、链接八、脚注九、图片插入十、表格十一、流程图十二、表情符号十三、数学公式的输入1.公式的插入①…...
读元宇宙改变一切笔记02_元素(上)
1. 很多组织和机构都想在元宇宙的定义上掌握话语权,使得它的定义中存在矛盾之处,也有大量含义混淆之处 1.1. 微软 1.1.1. 在谈论“多个元宇宙” 1.1.2. 微软首席执行官萨提亚纳德拉将元宇宙描述为一种可以将“整个…...
听GPT 讲Rust源代码--compiler(2)
File: rust/compiler/rustc_codegen_cranelift/build_system/prepare.rs 在Rust源代码中,rust/compiler/rustc_codegen_cranelift/build_system/prepare.rs文件的作用是为Cranelift代码生成器构建系统准备依赖项。 具体来说,该文件的主要目标是处理Crane…...
SpringCloud系列篇:核心组件之负载均衡组件
🥳🥳Welcome Huihuis Code World ! !🥳🥳 接下来看看由辉辉所写的关于SpringCloud的相关操作吧 目录 🥳🥳Welcome Huihuis Code World ! !🥳🥳 一.负载均衡组件是什么 二.负载均衡…...
多线程模板应用实现(实践学习笔记)
出处:B站码出名企路 个人笔记:因为是跟着b站的教学视频以及文档初步学习,可能存在诸多的理解有误,对大家仅供借鉴,参考,然后是B站up阳哥的视频,我是跟着他学。大家有兴趣的可以到b站搜索。加油…...
Linux系统中MYSQL重置密码(针对root忘记密码)
⼀ .进⼊MySql配置⽂件中 vi /etc/my.cnf 在最后⼀⾏添加免密码登陆: skip-grant-tables :wq 保存退出 ⼆.重启MySql service mysql restart 或 systemctl restart mysqld.service 三. 登陆数据库 mysql -uroot -p 让输⼊密码直接回⻋就可以 四.修改MySql密码 use mysql…...
蓝桥杯基础知识1 字母大小写转换
蓝桥杯基础知识1 字母大小写转换 isalpha()判断一个字符是否为字母。 isalnum()判断一个字符是否为十进制数字字符或者字母,是否属于a~ z或A~ Z或0~9。 isdigit() 判断一个字符是否是十进制数字字符。十进制数字是:0 1 2 3 4 5 6 7 8 9 isalnum()和isdig…...
攀登者1 - 华为OD统一考试
OD统一考试 分值: 100分 题解: Java / Python / C++ 题目描述 攀登者喜欢寻找各种地图,并且尝试攀登到最高的山峰。 地图表示为一维数组,数组的索引代表水平位置,数组的元素代表相对海拔高度。其中数组元素0代表地面。 例如:[0,1,2,4,3,1,0,0,1,2,3,1,2,1,0],代表如下…...
通信原理期末复习——基础小题汇总(二)
个人名片: 🦁作者简介:一名喜欢分享和记录学习的在校大学生 🐯个人主页:妄北y 🐧个人QQ:2061314755 🐻个人邮箱:2061314755qq.com 🦉个人WeChat:V…...
代码随想录刷题第四十二天| 01背包问题,你该了解这些! ● 01背包问题,你该了解这些! 滚动数组 ● 416. 分割等和子集
代码随想录刷题第四十二天 今天是0-1背包问题,掌握了套路就不难了~~~ 0-1背包问题理论基础(二维数组篇)卡码网第46题 题目思路: 代码实现: input_line input() # 读取一行输入 mn input_line.split() m, n int…...
前端开发加速器:十个VSCode插件精选
前端开发是一个不断发展的领域,随着技术的进步,工具也在不断更新。Visual Studio Code(VSCode)是前端开发者广泛使用的编辑器之一,得益于其强大的插件系统,可以帮助开发者提升工作效率。以下是十个对于前端…...
剑指offer面试题3 二维数组中的查找
考察点: 考察数据结构二维数组知识点: 1.java中的数据类型分为基本类型和引用类型,数组属于引用类型,引用类型的变量中存储的是地址,该地址指向内存中的某个对象,参考c中的指针。2.一维数组定义,…...
【2023年中国高校大数据挑战赛 】赛题 B DNA 存储中的序列聚类与比对 Python实现
【2023年中国高校大数据挑战赛 】赛题 B DNA 存储中的序列聚类与比对 Python实现 更新时间:2023-12-29 1 题目 赛题 B DNA 存储中的序列聚类与比对 近年来,随着新互联网设备的大量涌入和对其服务需求的指数级增长,越来越多的数据信息被产…...
力扣383.赎金信 -- 哈希表
思路:记录magazine每个字符个数,然后记录ransomNote每个字符(每有一个减1),假如出现<0的情况说明ransomnode有字符的个数超过了magazine则无法构成,否则可以构成 代码: class Solution { pu…...
GeoServer发布地图服务(WMS、WFS)
文章目录 1. 概述2. 矢量数据源3. 栅格数据源 1. 概述 我们知道将GIS数据大致分成矢量数据和栅格数据(地形和三维模型都是兼具矢量和栅格数据的特性)。但是如果用来Web环境中,那么使用图片这个栅格形式的数据载体无疑是最为方便的࿰…...
C语言——结构体
一、结构体的创建 1、定义 在 C 语言中,结构体是一种自定义的数据类型,它允许将不同类型的数据项组合成一个单一实体。这在组织复杂数据时非常有用,因为它可以将有逻辑关系的数据组合在一起。结构体是一些值的集合,这些值是结构…...
基于多反应堆的高并发服务器【C/C++/Reactor】(中)Buffer的创建和销毁、扩容、写入数据
TcpConnection:封装的就是建立连接之后得到的用于通信的文件描述符,然后基于这个文件描述符,在发送数据的时候,需要把数据先写入到一块内存里边,然后再把这块内存里边的数据发送给客户端,除了发送数据,剩下…...
【Linux】常用的基本命令指令①
前言:从今天开始,我们逐步的学习Linux中的内容,和一些网络的基本概念,各位一起努力呐! 💖 博主CSDN主页:卫卫卫的个人主页 💞 👉 专栏分类:数据结构 👈 💯代码…...
活动运营常用的ChatGPT通用提示词模板
活动目标确定:如何明确活动的目标,确保活动策划与执行的方向性? 活动主题选择:如何选择吸引人的活动主题,提高用户的参与度和兴趣? 活动形式策划:如何根据活动目标和主题,选择适合…...
SpringBoot 中实现订单30分钟自动取消的策略
简介 在电商和其他涉及到在线支付的应用中,通常需要实现一个功能:如果用户在生成订单后的一定时间内未完成支付,系统将自动取消该订单。 本文将详细介绍基于Spring Boot框架实现订单30分钟内未支付自动取消的几种方案,并提供实例…...
像专家一样使用TypeScript映射类型
掌握TypeScript的映射类型,了解TypeScript内置的实用类型是如何工作的。 您是否使用过Partial、Required、Readonly和Pick实用程序类型? 你知道他们内部是怎么运作的吗? 如果您想彻底掌握它们并创建自己的实用程序类型,那么不要错过本文所涵盖的内容。…...
Golang 结构体
前言 在 Go 语言中,结构体(struct)是一种自定义的数据类型,将多个不同类型的字段(fields)组合在一起 结构体通常用于模拟真实世界对象的属性和行为 定义结构体 可以使用 type 关键字和 struct 关键字来定…...
服务器运行状况监控工具
服务器运行状况监视提供了每个服务器状态和性能的广泛概述,通过监控服务器指标,如 CPU 使用率、内存消耗、I/O、磁盘使用率、进程等,服务器运行状况监控可以避免服务器停机。 服务器性能监控指标 服务器是网络中最重要的组件之一࿰…...
关键词优化是什么意思/独立站seo建站系统
2014年,HTML5页面作为营销界新宠儿,“多快好省”的杰出代表,其灵活性高、开发成本低且制作周期短的种种特性使其在移动营销领域大放异彩。 文/Teeya 此前,介绍了HTML5网页的基础知识、有哪些应用场景以及如何推广,反响…...
沈阳网站建设建设公司哪家好/创建属于自己的网站
6月28-29日,中国企业云服务大会,在上海小南国花园酒店正式召开。联想云、阿里云等众多产业内的领先品牌就企业云服务趋势、战略、大数据技术及应用等热门议题深度交流,并在展览区直观呈现云技术的落地成果。联想云存储副总经理张跃华在本次盛…...
微信h5制作网站开发/昆明装饰企业网络推广
https://blog.csdn.net/weixin_33912638/article/details/86048042 方法就是在工程属性里设置: 配置属性-〉常规-〉项目默认值-〉MFC的使用-〉在静态库中使用MFC,见下图 之后重新编译即可。...
wordpress页面内导航/文案代写
3、变频器实训挂箱配置西门子V20 0.37KW变频器,带有RS485通讯及BOP操作面板。4、触摸屏实训组件:7英寸昆仑通态,256色,了解工业触摸屏的功能及使用方法、掌握与PLC之间的通信知识,并掌握复位、置位、交替等功能键、图形…...
哪些网站可以做免费广告推广/网站技术解决方案
在大多数情况下,在软件项目上进行协作意味着使用Git之类的工具-轮流进行修改,然后将最终产品与单个代码库协调。 但是,近年来,在代码上进行实时协作(两个或多个人实时处理同一个文件)变得更加可行。 您仍然…...
济宁网站运营策略/产品推广广告
最方便的eclipse安装svn插件,没有之一,在eclipse插件市场安装即可: Help > Eclipse Marketplace... > 输入svn搜索插件安装 安装完成后,顶部菜单栏目就有SVN的菜单选项了...