PyTestReport-0.1.9版本更新

原文首發

還記得那個發佈不就的Python單元測試報告框架麼?噢!如果不記得那我今天就再來說一篇吧!_ PyTestReport 0.1.9版本正式發佈了!

多了哪些功能呢?

  • 增加對PyTest框架的支持(目前可以支持unittest、pytest兩大框架)
  • 增加了支持通過lib API接口的方式生成測試報告(前提是把報告數據整理成要求的格式)
  • 增加了HTML報告轉圖片報告的API
  • 增加了發送郵件功能的API
  • 修復了一些bug!這些是自然的!
  • 規範了一些數據格式的字段和內容

接下來,請坐好小板凳,我準備帶你們飛了!!!

如何與PyTest框架結合使用

對於pytest框架,收集其測試結果信息是通過pytest插件形式實現的。使用之前只要確保正常安裝了PyTestReport即可。具體使用方式如下:

import pytest

def testTrue():
    assert True

def testFalse():
    assert False

def testError():
    1 / 0

@pytest.mark.skip(reason="misunderstood the API")
def testSkip():
    assert 1 == 1

@pytest.mark.xfail(reason="Xpass")
def testXPass():
    assert True

@pytest.mark.xfail(reason="Xfail")
def testXFail():
    assert False


if __name__ == '__main__':
    pytest.main(["-s", "pytest_Demo.py", "--pytest_report", "Pytest_Report.html"])

需要注意的是,pytest框架想要使用本測試報告框架,在調用時需要帶上--pytest_report參數,並指定一個報告的文件路徑即可。當然你也可以同時指定一個非默認主題。比如:

import pytest

if __name__ == '__main__':
    pytest.main(["-s", "pytest_Demo.py", "--pytest_report", "Pytest_Report.html", 
    "--pytest_title", "report title", "--pytest_desc", "report desc",
    "--pytest_theme", "new_theme"])

另外,你也可以通過命令行的方式來啓動pytest執行單元測試。比如:

pytest -s pytest_Demo.py --pytest_report Pytest_Report.html --pytest_theme new_theme

如何通過API的方式生成報告

from pytestreport.api import make_report

data = {
    "generator": "PyTestReport 0.1.4",
    "title": "默認主題",
    "description": "默認主題描述",
    "report_summary": {
        "start_time": "2019-05-12 23:07:49",
        "duration": "0:00:00.002000",
        "suite_count": 1,
        "status": {
            "pass": 1,
            "fail": 0,
            "error": 0,
            "skip": 0,
            "count": 1
        }
    },
    "report_detail": {
        "tests": [
            {
                "summary": {
                    "desc": "utDemo.UTestPass",
                    "count": 1,
                    "pass": 1,
                    "fail": 0,
                    "error": 0,
                    "skip": 0,
                    "cid": "testclass1",
                    "status": "pass"
                },
                "detail": [
                    {
                        "has_output": False,
                        "tid": "testpass.1.1",
                        "desc": "testTrue",
                        "output": "",
                        "status": "pass",
                        "status_code": 0
                    }
                ]
            }
        ],
        "count": "1",
        "pass": "1",
        "fail": "0",
        "error": "0",
        "skip": "0"
    }
}
with open('API_Report.html', 'wb') as fp:
    make_report(fp, data)
# will be create API_Report.html file at current directory.

同樣的,你也可以指定特定的主題或者樣式。比如:

...
with open('API_Report.html', 'wb') as fp:
    make_report(fp, data, theme='new_theme', stylesheet='new_stylesheet_2.css')

如何生成圖片報告併發送郵件

from pytestreport.api import make_image, send_report

html = r"D:\test\report.html"
image = r"D:\test\report.png"
make_image(html, image)
# will be generate "report.png" file at "D:\test\". 

send_report("subject", html, image, "[email protected]", ["[email protected]"], cc=["[email protected]"])
# will be send image content email with html as attach accordingly.

新書推薦

Python Web自動化測試設計與實現

獲取更多關於Python和自動化測試的文章,請掃描如下二維碼!
關注二維碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章