04-13pytest的基本使用方法
引言
有了unittest这个经典的测试框架做基础,那么学习其他任何的测试框架都变得有章法可循了。
pytest测试框架也是由unittest改编而来,所以许多地方都是一脉相承。
pytest的运行也是多样化的。
正文:
4 运行测试
前面我们运行的时候,都是在terminal下,输入命令 pytest test_*.py来运行。
实际上pytest提供了不少其他运行指令,我们可以通过pytest –help查看。
这里列出常用的一些指令:
-k 运行名称中包含某字符的测试用例
-q 减少测试的运行冗长
-x 如果出现一天测试用例失败,则退出测试
-v 输出更详细的日志信息
下面是一个测试py文件
.\test_runrule.py
# 运行测试的案例说明模块
# 功能函数
def func(x,y):
return x+y >10
def func_math(x,y):
import math
return math.fabs(x)
def test_isnottrue():
assert func(3,3) is True
def test_func_math():
assert func_math(3.333,6.222) >0
我们运行上面的test_runrule.py
1.运行命令: pytest test_runrule.py
(testops) >pytest test_runrule.py
======================================================== test session starts ========================================================
platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: D:\Coding\Project\testops\Stage5\07pytest
collected 2 items
test_runrule.py F. [100%]
============================================================= FAILURES ==============================================================
__________________________________________________________ test_isnottrue ___________________________________________________________
def test_isnottrue():
> assert func(3,3) is True
E assert False is True
E + where False = func(3, 3)
test_runrule.py:13: AssertionError
==================================================== 1 failed, 1 passed in 0.03s ====================================================
2.运行命令: pytest -k "math" test_runrule.py
其中-k "math" 筛选只包含math的字符测试用例,结果如下,只运行了test_func_math,不运行test_isnottrue
(testops) >pytest -k "math" test_runrule.py
======================================================== test session starts ========================================================
platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: D:\Coding\Project\testops\Stage5\07pytest
collected 2 items / 1 deselected / 1 selected
test_runrule.py . [100%]
================================================== 1 passed, 1 deselected in 0.01s ==================================================
3.运行命令: pytest test_runrule.py -q
其中-q 是减少冗长
(testops) >pytest test_runrule.py -q
F. [100%]
============================================================= FAILURES ==============================================================
__________________________________________________________ test_isnottrue ___________________________________________________________
def test_isnottrue():
> assert func(3,3) is True
E assert False is True
E + where False = func(3, 3)
test_runrule.py:13: AssertionError
1 failed, 1 passed in 0.03s
4 运行命令: pytest test_runrule.py -x
其中-x 如果出现一条测试用例失败,则退出测试
(testops) >pytest test_runrule.py -x
======================================================== test session starts ========================================================
platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: D:\Coding\Project\testops\Stage5\07pytest
collected 2 items
test_runrule.py F
============================================================= FAILURES ==============================================================
__________________________________________________________ test_isnottrue ___________________________________________________________
def test_isnottrue():
> assert func(3,3) is True
E assert False is True
E + where False = func(3, 3)
test_runrule.py:13: AssertionError
========================================================= 1 failed in 0.03s =========================================================
-
运行命令: pytest test_runrule.py -v
-v 能把信息打印的更详细
(testops) >pytest test_runrule.py -v ======================================================== test session starts ======================================================== platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 -- d:\python\virtualenvs\testops\scripts\python.exe cachedir: .pytest_cache rootdir: D:\Coding\Project\testops\Stage5\07pytest collected 2 items test_runrule.py::test_isnottrue FAILED [ 50%] test_runrule.py::test_func_math PASSED [100%] ============================================================= FAILURES ============================================================== __________________________________________________________ test_isnottrue ___________________________________________________________ def test_isnottrue(): > assert func(3,3) is True E assert False is True E + where False = func(3, 3) test_runrule.py:13: AssertionError ==================================================== 1 failed, 1 passed in 0.03s ====================================================
-
运行命令 pytest .\
其中.\是文件目录的名字
(testops) >pytest .\ ======================================================== test session starts ======================================================== platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 rootdir: D:\Coding\Project\testops\Stage5\07pytest collected 22 items test_assert.py . [ 4%] test_introfixture.py . [ 9%] test_main.py F [ 13%] test_operator.py ........... [ 63%] test_parameterized.py .... [ 81%] test_pytestfixture.py . [ 86%] test_runrule.py F. [ 95%] test_sample.py F [100%] ============================================================= FAILURES ============================================================== ____________________________________________________________ test_answer ____________________________________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_main.py:7: AssertionError __________________________________________________________ test_isnottrue ___________________________________________________________ def test_isnottrue(): > assert func(3,3) is True E assert False is True E + where False = func(3, 3) test_runrule.py:13: AssertionError ____________________________________________________________ test_answer ____________________________________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_sample.py:6: AssertionError =================================================== 3 failed, 19 passed in 0.12s ====================================================
-
通过main()方法运行测试
import pytest if __name__== '__main__': pytest.main(['-s','./test_runrule.py'])
8.运行的时候生成html报告
#生成html报告在当前路径,css文件分离 pytest test_markers.py --html=report.html #生成html报告在当前路径,cs文件不分离 pytest test_markers.py --html=report.html --self-contained-html
# 先安装pytest-html 外部模块
pip install pytest-html
总结:
1 pytest的运行参数千千万,务必牢记 -v -s
2 运行入口有py文件,有class 类, 还有 function方法
3 pytest的html报告需要把css放入html文件中,才能分享出去的时候不丢失
思考与延伸
1 pytest的html报告中能显示用例描述吗?