1. 程式人生 > >Python單元測試框架pytest常用測試報告型別

Python單元測試框架pytest常用測試報告型別

先前部落格有介紹pytest測試框架的安裝及使用,現在來聊聊pytest可以生成哪些測試報告 #1.allure測試報告 關於allure報告參見先前的一篇博文:https://www.cnblogs.com/feng0815/p/13792188.html ,這裡不再贅述 #2.生成resultlog檔案 ```python #!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_report.py @time:2021/01/27 """ class TestReport: def test_one(self): x = "shifeng" assert "feng" in x def test_two(self): x = "hello" assert x == "hi" ``` 執行命令: ```shell pytest test_report.py --resultlog=./resultlog.txt ``` 指定當前路徑下生成resultlog.txt檔案,開啟檔案,內容如下: ```txt . reportdemo/test_report.py::TestReport::test_one F reportdemo/test_report.py::TestReport::test_two self = def test_two(self): x = "hello" > assert x == "hi" E AssertionError: assert 'hello' == 'hi' E - hi E + hello test_report.py:16: AssertionError ``` #3.生成JunitXML檔案 執行命令: ```shell pytest test_report.py --junitxml=./resultlog.xml ``` 同樣指定在當前目錄下生成resultlog.xml檔案,開啟檔案內容如下: ```xml
self = <test_report.TestReport object at 0x7fa152b97790> def test_two(self): x = "hello" > assert x == "hi" E AssertionError: assert 'hello' == 'hi' E - hi E + hello test_report.py:16: AssertionError
``` 建立這樣的XML檔案有有什麼用? 主要是為了方便Jenkin或其它的持續整合工具讀取。 #4.生成測試用例的URL 執行命令: ```shell pytest test_report.py --pastebin=all ``` ![](https://img2020.cnblogs.com/blog/1024536/202101/1024536-20210128000514430-653944129.png) 複製列印結果最後生成的session-log測試報告連結到瀏覽器:https://bpa.st/UW2IG ![](https://img2020.cnblogs.com/blog/1024536/202101/1024536-20210128000736491-1200664547.png) 當然,你也可以只選擇展示faile的測試用例 ```python pytest test_class.py --pastebin=failed ``` #5.生成html測試報告 通過pip安裝pytest-html ```shell pip install pytest-html ``` 在程式碼檔案的當前目錄下執行命令 ```shell pytest test_report.py --html=./report.html ``` ![](https://img2020.cnblogs.com/blog/1024536/202101/1024536-20210128003204917-978239891.png) 指定在當前目錄下生成report.html檔案,開啟測試檔案: ![](https://img2020.cnblogs.com/blog/1024536/202101/1024536-20210128003347662-6587043