1、安裝pytest-html
使用命令pip install pytest-html -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
來安裝pytest-html
官方文檔地址:pytest-html官方鏈接
2、書寫pytest測試用例
書寫測試用例文件Test_simple.py
:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
def test_case1(self):
tof = True
assert tof
def test_case2(self):
tof = False
assert tof
3、運行用例
使用命令行方式運行用例:
pytest --html=report/report.html
即可在pytest運行目錄下的report文件夾下看到report.html和一個包含css文件的文件夾。
但是這種方式無法將這個html文件放入郵件正文。故可以用如下方式來生成一個包含css樣式的html格式報告:
pytest --html=report/report.html --self-contained-html
這樣就只有一個html文件,如下:
測試報告結果html如下: