1. 程式人生 > 實用技巧 >Pytest-HTML 官方文件

Pytest-HTML 官方文件

一、官網資料 安裝 要安裝 pytest-html:
$ pip install pytest-html
然後執行你的測試:
$ pytest --html=report.html

建立自包含報表

為了遵守內容安全策略( CSP ) 外掛,預設情況下,將分別儲存CSS和影象等若干資產。可以選擇建立自包含報表,這在共享結果時更方便。 可以按以下方式執行這裡操作:
$ pytest --html=report.html --self-contained-html
作為檔案或者連結新增的影象將被連結為外部資源,這意味著獨立報表html檔案可能不會像預期那樣顯示這些影象。 外掛將在新增檔案或者連結到獨立報表時發出警告。

增強報告

環境

web - config -環境部分由pytest元資料外掛提供,並可以通過pytest_configure鉤子訪問:
defpytest_configure(config):
config._metadata['foo'] ='bar'

額外內容

你可以在報表物件上建立'額外'列表,從而向HTML報告新增詳細資訊。 以下是可以新增的額外內容型別: 型別示例
原始 HTML extra.html('<div>Additional HTML</div>')
JSON extra.json({'name': 'pytest'})
純文字 extra.text('Add some simple Text')
URL extra.url('http://www.example.com/')
影象 extra.image(image, mime_type='image/gif', extension='gif')
影象 extra.image('/path/to/file.png')
影象 extra.image('http://some_image.png')
註釋:當從檔案中新增影象時,路徑可以是絕對的,也可以是 relative。 注意: 使用--self-contained-html時,新增為檔案或者連結的影象可能無法按預期工作,有關更多資訊,請參見建立自包含的報表。 對於幾種影象格式也有方便的型別: 影象格式示例
PNG extra.png(image)
JPEG extra.jpg(image)
SVG extra.svg(image)
以下示例使用pytest_runtest_makereport鉤子新增各種型別的附加項,可以在外掛或者 conftest.py file: 中實現這些附加項
 1 @pytest.hookimpl(hookwrapper=True)
 2 def pytest_runtest_makereport(item, call):
 3     pytest_html = item.config.pluginmanager.getplugin('html')
 4     outcome = yield
 5     report = outcome.get_result()
 6     extra = getattr(report, 'extra', [])
 7     if report.when == 'call':
 8         # always add url to report
 9         extra.append(pytest_html.extras.url('http://www.baidu.com/'))
10         xfail = hasattr(report, 'wasxfail')
11         if (report.skipped and xfail) or (report.failed and not xfail):
12             # only add additional html on failure
13             extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
14         report.extra = extra

你還可以為除html之外的所有型別指定name引數,這將更改建立的超級連結的標題:

extra.append(pytest_html.extras.text('some string', name='Different title'))
二、Allure Report本地開啟報錯 1.無法本地開啟,可以通過pycharm開啟,pycharm自帶容器服務,開啟一個埠執行。 2.Anywhere執行 Anywhere是一個隨啟隨用的靜態伺服器,它可以隨時隨地將你的當前目錄變成一個靜態檔案伺服器的根目錄。 安裝之後,這個執行最簡單,在report目錄下開啟cmd,輸入anywhere就可以啟動一個服務 三、參考連結 原始碼網址:http://www.github.com/pytest-dev/pytest-html 中文翻譯:https://www.kutu66.com/GitHub/article_123019 Python中文文件: https://www.cnpython.com/pypi/pytest-html