【python学习】单元测试框架pytest(二)-29

pytest:编写测试用例 - 收集测试用例 - 执行测试用例 - 生成测试报告

编写测试用例:
   用例名称、用例步骤、预期结果 、实际结果 、前置后置
   1、用例名称:要以test_开头
   2、断言assert:(实际和预期的比对) assert 表达式(True/False)

自动收集测试用例:
    1、收集用例的目录:以rootdir作为根目录。从rootdir目录下开始搜索用例。
    2、目录下的文件过滤:文件名以test_开头的py文件,或者文件名以_test结尾的py文件。
    3、文件下的用例过滤:.py下的函数,函数名以test_开头/.py下类(Test开头)里面的方法,方法名以test_开头

如:test_case01.py

from random import randint

def random_num():
    return randint(1,10)

def test_1():
    print("第一条测试用例")
    assert random_num() == 2

def test_2():
    print("第二条测试用例")
    assert random_num() == 4

def test_3():
    print("第三条测试用例")
    assert random_num() == 6

def test_4():
    print("第四条测试用例")
    assert random_num() == 8

def test_5():
    print("第五条测试用例")
    assert random_num() == 10

def test_6():
    print("第六条测试用例")
    assert random_num() == 1

在根目录下,在创建一个py文件,如:当前取名为:test.py

注:当执行test.py文件后,会自动去寻找当前根目录下包含test_开头的文件,并且执行py文件里面的测试用例,并输出测试报告

import pytest
pytest.main(["-s","-v","--html=测试报告.html","--alluredir=allure-report-files"])

输出的html测试报告如下:

 html报告 - html插件
        1、pip install pytest-html
        2、pytest命令加入参数:--html=报告路径(相对于rootdir)) 

-------------------------------------------------------------------------------------------------------------------------------

 可以通过pycharm控制台,对测试用例文件进行执行:

1、选择Terminal,然后进入当前文件目录下

2、在控制台输入:pytest

 输出后的结果如下:

(当前执行结果:5条失败,1条成功)

猜你喜欢

转载自blog.csdn.net/admins_/article/details/122156375