Python單元測試框架Pytest——如何生成測試報告
阿新 • • 發佈:2019-01-23
本文講述pytest如何生成測試報告。
首先準備一段測試程式碼:
import py.test class TestCase(object): def test_eq_set(self): assert set([0, 10, 11, 12]) == set([0, 20, 21]) def test_eq_dict(self): assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0} def test_eq_list(self): assert [0, 1, 2] == [0, 1, 3] def test_eq_longer_list(self): assert [1,2] == [1,2,3]
1、文字格式的報告
C:\Users\liu.chunming\Desktop>py.test test_report.py --resultlog=C:\Users\liu.chunming\Desktop\log.txt
指定當前路徑下生成log.txt檔案,開啟檔案,內容如下:
2、生成JUnitXml格式報告
該格式方便與CI伺服器進行整合。
C:\Users\liu.chunming\Desktop>py.test test_report.py --junitxml=C:\Users\liu.chunming\Desktop\log.xml
開啟生成的log.xml,內容如下:
3、將測試報告發送到pastebin伺服器
C:\Users\liu.chunming\Desktop>py.test test_report.py --pastebin=all
點選生成的網址,內容如下:
當然,你可以只將失敗的報告發送到pastebin伺服器
C:\Users\liu.chunming\Desktop>py.test test_report.py --pastebin=failed
4、生成Html格式報告
這個需要安裝pytest的第三方外掛pytest-html:執行測試:C:\Users\liu.chunming\Desktop>pip install -U pytest-html
C:\Users\liu.chunming\Desktop>py.test test_report.py --html=C:\Users\liu.chunming\Desktop\log.html
開啟生成的測試報告log.html: