[英]How to pass fixture object to pytest_runtest_makereport?
We use the pytest library for automation test.我们使用 pytest 库进行自动化测试。 We need to take screen shots for fail case.
我们需要为失败的情况拍摄屏幕截图。 I want to use
mydriver
variable in pytest_runtest_makereport
method to take screen shots with this variable for fail case.我想在
pytest_runtest_makereport
方法中使用mydriver
变量来使用此变量拍摄失败情况的屏幕截图。 How can I do this?我怎样才能做到这一点?
Additional: We don't use the conftest.py file.附加:我们不使用 conftest.py 文件。 Can we use the
pytest_runtest_makereport
method without conftest.py?我们可以在没有 conftest.py 的情况下使用
pytest_runtest_makereport
方法吗? We use just a sampleTest.py file.我们只使用一个 sampleTest.py 文件。
sampleTest.py:样本测试.py:
import time
from appium import webdriver
import pytest
@pytest.yield_fixture(scope="function")
def driver():
"""Setup for the test"""
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '11.0'
desired_caps['deviceName'] = 'Samsung Galaxy A70'
# Since the app is already installed launching it using package and activity name
desired_caps['appPackage'] = 'com.example.mapkitbaseline'
desired_caps['appActivity'] = 'com.example.mapkitbaseline.ui.CreationMapActivity'
mydriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
yield mydriver
mydriver.quit()
@pytest.mark.usefixtures("driver")
def test_func1(driver):
""""Testing the HMS MapKit demo app"""
driver.implicitly_wait(30)
time.sleep(5)
# Assert that MapKit->.ui.CreationMapActivity->btnMapCreation4 button text is equal to "suuportfragmentx"
elmnt = driver.find_element_by_id('com.example.mapkitbaseline:id/btnMapCreation4')
assert elmnt.get_attribute('text').lower() == "supportmapfragmentfffffffffx"
print(elmnt.get_attribute('text'))
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == "call":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# I need to use mydriver variable this here. This code doesn't work.
screenshot = mydriver.get_screenshot_as_base64()
extra.append(pytest_html.extras.image(screenshot, ''))
# extra.append(pytest_html.extras.html('<style> #results-table{ position: relative; position:absolute !important;top:300px !important; left:100px !important; width: 50% !important; z-index: -1 !important;}</style>'))
report.extra = extra
You can access fixtures in item.funcargs
.您可以在
item.funcargs
访问设备。
if 'driver' in item.fixturenames: # Wrap in this
mydriver = item.funcargs['driver'] # Add this
screenshot = mydriver.get_screenshot_as_base64()
extra.append(pytest_html.extras.image(screenshot, ''))
Reference: https://docs.pytest.org/en/6.2.x/example/simple.html#post-process-test-reports-failures参考: https : //docs.pytest.org/en/6.2.x/example/simple.html#post-process-test-reports-failures
pytest_runtest_makereport
without conftest.pypytest_runtest_makereport
没有conftest.py It's possible, but you probably shouldn't do it.这是可能的,但你可能不应该这样做。
# @pytest.mark.hookwrapper # Replace this
@pytest.hookimpl(hookwrapper=True) # with this
def pytest_runtest_makereport(item, call):
# ...
import gc
import sys
from pluggy.hooks import HookImpl
from _pytest.config import Config
config = next(o for o in gc.get_objects() if isinstance(o, Config))
hookimpl = HookImpl(sys.modules[__name__], __file__, pytest_runtest_makereport, pytest_runtest_makereport.pytest_impl)
config.hook.pytest_runtest_makereport._add_hookimpl(hookimpl)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.