pytest-自动生成测试报告
1.windows安装pytest插件:
pip install pytest pip install pytest-html pip install pytest-rerunfailures pip install allure-pytest
2.编写测试用例执行代码
import pytest
# 定义前置处理,设置autouse=True后,pytest会自动在测试方法前调用,不需要测试方法声明要使用该fixture
# 加一个前置 是True的话使用的时候不用传参数,可以直接使用,并且每次都会执行
@pytest.fixture(autouse=True)
def beforeEach():
print("我是beforeEach fixture")
# 定义前置处理,需要测试方式声明该fixture才会在测试方法之前调用
# 不需要测试方法声明要使用该fixture
@pytest.fixture()
def before():
print("我是before fixture")
'''
定义后置处理器after,
'''
@pytest.fixture()
def after():
yield
print("我是after fixture")
# 参数传进去了才会被使用
# 使用参数方式,声明使用上面定义的before fixture
def test_01(before):
print("我是test001")
@pytest.mark.usefixtures('before')
def test_02():
print("我是test002")
def test_03(before,after):
print("我是test03")
3.在DOS窗口下执行生成测试报告代码
4.生成测试报告
注意生成测试报告的命令必须在python测试用例执行的路径下执行才能够正确生成html格式的测试报告(如下:)

pytest -v --html=report/report.html

浙公网安备 33010602011771号