1 回答

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
我找到了一個(gè)解決方法,雖然它并不漂亮。
在我的測(cè)試文件中在模塊級(jí)別添加一個(gè)變量,允許我使用item.module.varname,所以如果我varname在我的模塊測(cè)試中設(shè)置,然后在測(cè)試中分配它;我可以訪(fǎng)問(wèn)它pytest_runtest_makereport
在測(cè)試文件.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
添加回答
舉報(bào)