Python單元測試框架之pytest -- 生成測試報告


 

  繼續pytest單元測試框架的學習,pytest可以生成多種類型的測試報告。這一節就來學習pytest如何生成測試報告。

  創建test_calss.py 測試用例文件,這里以測試該文件為例。

#coding=utf-8

class TestClass: def test_one(self): x = "this"
        assert "h" in x def test_two(self): x = "hello"
        assert x == "hi"

 

 

生成resultlog文件                                                         

 創建普通的結果文件:

> py.test test_class.py  --resultlog=./log.txt

  指定當前路徑下生成log.txt文件,打開文件,內容如下:

. test_class.py::TestClass::()::test_one F test_class.py::TestClass::()::test_two self = <test_class.TestClass instance at 0x000000000307C788> def test_two(self): x = "hello" > assert x == "hi" E assert 'hello' == 'hi' E - hello E + hi test_class.py:11: AssertionError

 

 

生成JunitXML文件                        

> py.test test_class.py  --junitxml=./log.xml

  同樣指定在當前目錄下生成log.xml文件,打開文件內容如下:

<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.015">
    <testcase classname="test_class.TestClass" name="test_one" time="0.0"/>
    <testcase classname="test_class.TestClass" name="test_two" time="0.00300002098083">
        <failure message="assert &apos;hello&apos; == &apos;hi&apos; - hello + hi">self = &lt;test_class.TestClass instance at 0x000000000309C948&gt; def test_two(self): x = &quot;hello&quot;
        &gt;           assert x == &quot;hi&quot; E assert &apos;hello&apos; == &apos;hi&apos; E - hello E + hi test_class.py:11: AssertionError </failure>
    </testcase>
</testsuite>

  創建這樣的XML文件有有什么用? 主要是為了方便Jenkin或其它的持續集成工具俱讀取。

 

 

創建測試用例的URL                                                

> py.test test_class.py  --pastebin=all

復制打印結果最后生成的session-log測試報告鏈接到瀏覽器:

https://bpaste.net/show/4815ce13c164

這樣的結果展示將非常友好。

 

當然,你也可以只選擇展示faile的測試用例

> py.test test_class.py  --pastebin=failed

 

 

生成html測試報告                              

  當然,更多時候,我們希望pytest能生成漂亮的測試報告。這需要安裝pytest的擴展--pytest-html

> pip install pytest-html     # 通過pip安裝pytest-html

 

cmd命令提示符下執行測試文件:

>py.test test_class.py --html=./report.html

指定在當前目錄下生成report.html文件,打開測試文件:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM