【问题标题】:How to pass fixture object to pytest_runtest_makereport?如何将夹具对象传递给 pytest_runtest_makereport?
【发布时间】:2021-12-08 00:34:51
【问题描述】:

我们使用 pytest 库进行自动化测试。我们需要为失败案例拍摄屏幕截图。我想在pytest_runtest_makereport 方法中使用mydriver 变量来使用此变量进行屏幕截图以用于失败情况。我该怎么做?

附加:我们不使用 conftest.py 文件。我们可以在没有 conftest.py 的情况下使用pytest_runtest_makereport 方法吗?我们只使用一个 sampleTest.py 文件。

sampleTest.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

【问题讨论】:

    标签: python automated-tests pytest fixtures


    【解决方案1】:

    您可以在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, ''))
    

    参考:https://docs.pytest.org/en/6.2.x/example/simple.html#post-process-test-reports-failures


    使用pytest_runtest_makereport 不带conftest.py

    这是可能的,但你可能不应该这样做。

    # @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)
    

    【讨论】:

    • 我已经尝试过 conftest.py 。我遇到这样的错误:原始错误:'GET /screenshot' 无法代理到 UiAutomator2 服务器,因为检测进程未运行(可能已崩溃)。
    • 嗯,我不知道如何重现。
    • 那是因为当makereport hookimpl 调用并且mydriver.quit() 已经被调用时,fixtures 已经拆除了。
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多