pytest框架初识

安装两个库

pip install pytest

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

pytest使用规则:

1、文件名必须test_开头或_test结尾

2、类名必须Test开头

3、方法名必须test开头

4、断言用assert

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

1、testcase.py 

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

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

 testcase.py ..   

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

2、再增加一个函数

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

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

testcase.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 testcase.py::test03 - assert 4 == 5
======================== 1 failed, 2 passed in 0.33s =========

报告看一眼:

原文地址:https://www.cnblogs.com/docstrange/p/14764162.html