pytest 生成報告,需要提前安裝插件
pip install pytest-html
使用方式:
在運行時使用--html=report.html (report就是生成html的文件名)
eg:pytest test_rundemo.py --html=reportdemo.html
測試案例:做了一個計算器,然后斷言一個失敗
class Calc(object): @classmethod def add(cls, x, y, *d): # 加法計算 result = x + y for i in d: result += i return result @classmethod def sub(cls, x, y, *d): # 減法計算 result = x - y for i in d: result -= i return result @classmethod def mul(cls, x, y, *d): # 乘法計算 result = x * y for i in d: result *= i return result @staticmethod def div(x, y, *d): # 除法計算 if y != 0: result = x / y else: return -1 for i in d: if i != 0: result /= i else: return -1 return result def test_add(): assert Calc.add(1, 2, 3) == 60 #斷言失敗 def test_sub(): assert Calc.sub(100, 20, 30) == 50
控制台運行效果
在項目統計目錄生成報告文件