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文件,打開測試文件:

 PS:記得執行 java -jar selenium-server-standalone-2.25.0.jar

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json