Pytest拥有丰富的插件架构,超过800个以上的外部插件和活跃的社区,在PyPI项目中以” pytest- *”为标识。
本篇将列举github标星超过两百的一些插件进行实战演示。
1、
一次完整的测试,测试报告是必不可少的,但是pytest自身的测试结果过于简单,而pytest-html正好可以给你提供一份清晰报告。
安装:
pip install -U pytest-html
用例:
test_sample.py
importpytest#import time
被测功能
defadd(x, y):#time.sleep(1)
return x +y#测试类
classTestLearning:
data=[
[3, 4, 7],
[-3, 4, 1],
[3, -4, -1],
[-3, -4, 7],
]
@pytest.mark.parametrize(“data”, data)deftest_add(self, data):assert add(data[0], data[1]) == data[2]
运行:
E:\workspace-py\Pytest>pytest test_sample.py –html=report/index.html========================================================================== test session starts ==========================================================================platform win32– Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0collected4items
test_sample.py …F [100%]=============================================================================== FAILURES ================================================================================___________ TestLearning.test_add[data3] ______________
self= , data = [-3, -4, 7]
@pytest.mark.parametrize(“data”, data)
def test_add(self, data):> assert add(data[0], data[1]) == data[2]
E assert-7 == 7
E + where -7 = add(-3, -4)
test_sample.py:20: AssertionError————————————————- generated html file: file://E:\workspace-py\Pytest\report\index.html ————————————————–
======================================================================== short test summary info ========================================================================FAILED test_sample.py::TestLearning::test_add[data3]- assert -7 == 7
====================================================================== 1 failed, 3 passed in 0.14s ======================================================================
运行完,会生产一个html文件 和 css样式文件夹assets,用浏览器打开html即可查看清晰的测试结果。
后面我将会更新更加清晰美观的测试报告插件:
2、
在做单元测试时,代码覆盖率常常被拿来作为衡量测试好坏的指标,甚至,用代码覆盖率来考核测试任务完成情况。
安装:
pip install -U pytest-cov
运行:
E:\workspace-py\Pytest>pytest –cov=.========================================================================== test session starts ==========================================================================platform win32– Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0collected4items
test_sample.py …. [100%]———– coverage: platform win32, python 3.7.3-final-0 ———–Name Stmts Miss Cover————————————conftest.py5 3 40%test_sample.py7 0 100%
Original: https://blog.csdn.net/weixin_42195569/article/details/113515413
Author: 雪蔻
Title: python自动测试n_Pytest自动化测试 – 必知必会的一些插件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/775293/
转载文章受原作者版权保护。转载请注明原作者出处!