pytest框架测试用例的命名规则

使用pytest框架前需要安装两个库

pip install pytest

pip install pytest-html  原生态报告    (可执行pytest test_case.py -s --html=report.html  就会在代码同级目录下生成report.html报告,其中-s是把打印的内容输出到控制台)

pytest命名规则:

1、文件名必须test_开头或_test结尾(必须要有_,否则pytest不认为是用例)

2、类名必须Test开头(但建议加_,增加可读性)

3、方法名必须test开头(但建议加_,增加可读性)

4、断言用assert

 

 

 ---------------------举个例子-------------

1、test_case.py 

def auto(a):
    return a+1
def test_01():
    assert auto(1)==2
def test_02():
    assert auto(2)==3

然后可以在pycharm的终端执行:pytest test_case.py ,结果如下,因为两个案例都通过,所以显示两个点

 test_case.py ..   

=================================== 2 passed in 0.07s ===================

2、再增加一个函数

def test03():
    assert auto(3)==5

然后可以在pycharm的终端执行(run), 结果如下,因为第三个没通过,所以显示红色F,以及标识红色的都指出代码断言错误的地方了

test_case.py ..  

========================= FAILURES ======
___________________________ test03 __________

def test03():
> assert auto(3)==5
E assert 4 == 5
E + where 4 = auto(3)

testcase.py:12: AssertionError
======================= short test summary info ===========
FAILED test_case.py::test03 - assert 4 == 5
======================== 1 failed, 2 passed in 0.33s =========

 

3.生成报告,在命令行输入:pytest test_case.py -s --html=report1.html ,如下图:

 

 

 

在同级目录下就会自动生成html文件,可双击打开

 

 

 

 右键,点击open in browser ,再选择你电脑的浏览器客户端,点击打开如图

 

 

posted @ 2021-05-13 15:21  七亿星空  阅读(140)  评论(0编辑  收藏  举报