亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pytest html - 將圖像從測試文件傳遞到 conftest.py 中的掛鉤

pytest html - 將圖像從測試文件傳遞到 conftest.py 中的掛鉤

狐的傳說 2022-12-06 15:31:57
我有一個通過 pytest-html 生成 HTML 輸出的測試。我收到了報告,但我想添加對失敗和預期圖像的引用;我將它們保存在我的主 test.py 文件中,并將鉤子添加到conftest.py.現在,我不知道如何將這些圖像傳遞給函數;執行測試后調用掛鉤;目前我正在對輸出文件進行硬編碼并附加它們;但我想從測試中傳遞圖像的路徑,特別是因為我需要編寫更多測試,這些測試可能保存在我常用文件夾的其他地方,并且可能有不同的名稱。這是我在 conftest.py 中的鉤子@pytest.mark.hookwrapperdef pytest_runtest_makereport(item, call):    timestamp = datetime.now().strftime('%H-%M-%S')    pytest_html = item.config.pluginmanager.getplugin('html')    outcome = yield    report = outcome.get_result()    extra = getattr(report, 'extra', [])    if report.when == 'call':        # Attach failure image, hardcoded...how do I pass this from the test?        extra.append(pytest_html.extras.image('/tmp/image1.png'))        # test report html        extra.append(pytest_html.extras.url('http://www.theoutput.com/'))        xfail = hasattr(report, 'wasxfail')        if (report.skipped and xfail) or (report.failed and not xfail):            # only add additional data on failure            # Same as above, hardcoded but I want to pass the reference image from the test            extra.append(pytest_html.extras.image('/tmp/image2.png'))            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))        report.extra = extra我如何從我的 pytest 測試文件傳遞給鉤子,一個包含要附加的圖像路徑的變量?
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

我找到了一個解決方法,雖然它并不漂亮。


在我的測試文件中在模塊級別添加一個變量,允許我使用item.module.varname,所以如果我varname在我的模塊測試中設置,然后在測試中分配它;我可以訪問它pytest_runtest_makereport


在測試文件.py


import pytest


myvar1 = None

myvar2 = None


class VariousTests(unittest.TestCase):


    def test_attachimages():


        global myvar1

        global myvar2


        myvar1 = "/tmp/img1.png"

        myvar2 = "/tmp/img2.png"

在 conftest.py 中


@pytest.mark.hookwrapper

def pytest_runtest_makereport(item, call):


    timestamp = datetime.now().strftime('%H-%M-%S')


    pytest_html = item.config.pluginmanager.getplugin('html')

    outcome = yield

    report = outcome.get_result()

    extra = getattr(report, 'extra', [])

    if report.when == 'call':

        # Attach failure image

        img1 = item.module.myvar1

        img2 = item.module.myvar2

        extra.append(pytest_html.extras.png(img1))

        # test report html

        extra.append(pytest_html.extras.url('http://www.theoutput.com/'))

        xfail = hasattr(report, 'wasxfail')

        if (report.skipped and xfail) or (report.failed and not xfail):

            # only add additional data on failure

            # Same as above, hardcoded but I want to pass the reference image from the test

            extra.append(pytest_html.extras.png(img2))

            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))

        report.extra = extra


查看完整回答
反對 回復 2022-12-06
  • 1 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號