pytest有好几种测试报告插件,可以自己选择哪个好看用哪个。
1、pytest-html
安装
pip install pytest-html
使用
pytest.main(["--html=./report.html","--self-contained-html"])
import pytestfrom pytest_jsonreport.plugin import JSONReportclass TestCase:# 指定优先级@pytest.mark.run(order=11)def test_print(self):print("test_print")@pytest.mark.run(order=10)def test_hello(self):print("test_hello")@pytest.mark.run(order=3)def test_normal(self): # 可以加多个标签print("test_normal")def test_user_class(self):print("test_user_class")class TestCase2:@pytest.mark.onlinedef test_mygod(self):print("test_mygod!!!")@pytest.mark.smoke@pytest.mark.onliedef test_my(self):print("test_my!!!!")class TestCase3:def test_car(self):print("test_car!!!")def test_car1(self):print("test_car!!!")def test_fly(self):print("test_fly!!!!")@pytest.mark.smokedef test_user(): # 可以加多个标签print("test_user")if __name__ == '__main__':plugin=JSONReport()pytest.main([__file__,"--html=./report4.html","--self-contained-html","--json-report-file=none",],plugins=[plugin]) #assets文件删掉就看不来了,加上--self-contained-html需要assertprint(plugin.report)summary = plugin.report.get("summary")passed = summary.get("passed", 0)failed = summary.get("failed", 0)skipped = summary.get("skipped", 0)total = summary.get("total", 0)print("共{}条,通过{}条,失败{}条,跳过{}条".format(total, passed, failed, skipped))# pytest.main([__file__,"--html=./report2.html","--self-contained-html"]) #assets文件删掉就看不来了,加上--self-contained-html需要assert# pytest.main([__file__,"-s"]) #如果什么也不指定,从当前目录下所有文件夹里面搜索case# pytest.main(["-q",__file__]) #安静模式,不会显示pytest版本信息等等# pytest.main(["-v",__file__]) #详细模式,会打印每条case执行结果,指定python文件# pytest.main(["-s",__file__]) #展示模式,函数里面有print或者日志输出,会展示,指定python文件# pytest.main(["-vs",__file__]) #展示模式+详细模式,指定python文件# pytest.main(["-vs","./cases"]) #展示模式+详细模式,指定目录# pytest.main(["./cases","-k","login"]) #指定运行包含login关键字的,包括 py文件、类、函数# pytest.main(["./cases","-k","login or register"]) #指定运行包含login或者register关键字的,包括 py文件、类、函数# pytest.main(["./cases","-k","login and online"]) #指定运行包含login并且包含online关键字的,包括 py文件、类、函数# pytest.main(["./cases","-m","smoke"]) #指定运行smoke标签的case# pytest.main(["./cases","-m","smoke or online"]) #指定运行smoke或者online标签的case# pytest.main(["./cases","-m","smoke and online"]) #指定运行smoke是并且是online标签的case# pytest.main(["./cases", "-sk", "user", "-m", "smoke"]) # 指定运行名称中包含user的,标签是smoke的case
2、pytest新的报告样式:PyTestReport
安装第三方插件:
pip install PyTestReport
使用
pytest.main(["--pytest_report","new_pytest_report.html","--pytest_title","这是报名标题","--pytest_desc","这是报告内容",__file__])
3.pytest-html-reporter
安装:
pip install pytest-html-reporter
使用
pytest.main(["--html-report=report_new.html",__file__])
报告样式
