pytest有好几种测试报告插件,可以自己选择哪个好看用哪个。

1、pytest-html

安装

  1. pip install pytest-html

使用

  1. pytest.main(["--html=./report.html","--self-contained-html"])
  1. import pytest
  2. from pytest_jsonreport.plugin import JSONReport
  3. class TestCase:
  4. # 指定优先级
  5. @pytest.mark.run(order=11)
  6. def test_print(self):
  7. print("test_print")
  8. @pytest.mark.run(order=10)
  9. def test_hello(self):
  10. print("test_hello")
  11. @pytest.mark.run(order=3)
  12. def test_normal(self): # 可以加多个标签
  13. print("test_normal")
  14. def test_user_class(self):
  15. print("test_user_class")
  16. class TestCase2:
  17. @pytest.mark.online
  18. def test_mygod(self):
  19. print("test_mygod!!!")
  20. @pytest.mark.smoke
  21. @pytest.mark.onlie
  22. def test_my(self):
  23. print("test_my!!!!")
  24. class TestCase3:
  25. def test_car(self):
  26. print("test_car!!!")
  27. def test_car1(self):
  28. print("test_car!!!")
  29. def test_fly(self):
  30. print("test_fly!!!!")
  31. @pytest.mark.smoke
  32. def test_user(): # 可以加多个标签
  33. print("test_user")
  34. if __name__ == '__main__':
  35. plugin=JSONReport()
  36. pytest.main([__file__,"--html=./report4.html","--self-contained-html","--json-report-file=none",],plugins=[plugin]) #assets文件删掉就看不来了,加上--self-contained-html需要assert
  37. print(plugin.report)
  38. summary = plugin.report.get("summary")
  39. passed = summary.get("passed", 0)
  40. failed = summary.get("failed", 0)
  41. skipped = summary.get("skipped", 0)
  42. total = summary.get("total", 0)
  43. print("共{}条,通过{}条,失败{}条,跳过{}条".format(total, passed, failed, skipped))
  44. # pytest.main([__file__,"--html=./report2.html","--self-contained-html"]) #assets文件删掉就看不来了,加上--self-contained-html需要assert
  45. # pytest.main([__file__,"-s"]) #如果什么也不指定,从当前目录下所有文件夹里面搜索case
  46. # pytest.main(["-q",__file__]) #安静模式,不会显示pytest版本信息等等
  47. # pytest.main(["-v",__file__]) #详细模式,会打印每条case执行结果,指定python文件
  48. # pytest.main(["-s",__file__]) #展示模式,函数里面有print或者日志输出,会展示,指定python文件
  49. # pytest.main(["-vs",__file__]) #展示模式+详细模式,指定python文件
  50. # pytest.main(["-vs","./cases"]) #展示模式+详细模式,指定目录
  51. # pytest.main(["./cases","-k","login"]) #指定运行包含login关键字的,包括 py文件、类、函数
  52. # pytest.main(["./cases","-k","login or register"]) #指定运行包含login或者register关键字的,包括 py文件、类、函数
  53. # pytest.main(["./cases","-k","login and online"]) #指定运行包含login并且包含online关键字的,包括 py文件、类、函数
  54. # pytest.main(["./cases","-m","smoke"]) #指定运行smoke标签的case
  55. # pytest.main(["./cases","-m","smoke or online"]) #指定运行smoke或者online标签的case
  56. # pytest.main(["./cases","-m","smoke and online"]) #指定运行smoke是并且是online标签的case
  57. # pytest.main(["./cases", "-sk", "user", "-m", "smoke"]) # 指定运行名称中包含user的,标签是smoke的case

image.png

2、pytest新的报告样式:PyTestReport

安装第三方插件:

  1. pip install PyTestReport

使用

  1. pytest.main(["--pytest_report","new_pytest_report.html","--pytest_title","这是报名标题","--pytest_desc","这是报告内容",__file__])

报告样式
image.png

3.pytest-html-reporter

安装:

  1. pip install pytest-html-reporter

使用

  1. pytest.main(["--html-report=report_new.html",__file__])

报告样式
pytest:生成报告 - 图3