pytest挂钩可以找到固定装置

发布于 2025-01-24 06:32:07 字数 1828 浏览 4 评论 0原文

我正在尝试使用pytest_runtest_makereport挂钩来生成截然报告的屏幕截图,但由于'if'false = false在此代码中:

 if 'setup' in item.fixturenames:
                        web_driver = item.funcargs['setup']
                    else:
                        print('Fail to take screenshot.No setup fixture found')

conftest.py:

  @pytest.fixture(scope='function')
def get_webdriver(get_edge_options):
    options = get_edge_options
    print("Current working dir : %s" % os.getcwd())
    s = Service('D:\mmanager\msedgedriver.exe')
    driver = webdriver.Edge(service=s, options=options)
    # driver.delete_all_cookies()
    return driver


@pytest.fixture(scope='function')  # function means run each test in new browser session
def setup(request, get_webdriver):
    driver = get_webdriver
    if request.cls is not None:
        request.cls.driver = driver
    driver.get(FCC_HOME)
    yield driver
    driver.quit()


# Shared Given Steps

@given('the MM3-0 login page is displayed', target_fixture='MM_Login_page')
def MM_Login_page(setup):
    pass



@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if (rep.when == 'call' or rep.when == 'setup') and (rep.failed or rep.skipped):
        try:
            if 'setup' in item.fixturenames:
                web_driver = item.funcargs['setup']
            else:
                print('Fail to take screenshot.No setup fixture found')
                return
            allure.attach(
                web_driver.get_screenshot_as_png(),
                name='!! Screenshot Captured !!',
                attachment_type=allure.attachment_type.PNG)
        except Exception as e:
            print('Fail to take screen-shot: {}'.format(e))

I'm trying to generate a screenshot for the allure report using the pytest_runtest_makereport hook but get stuck because 'if' statement=false in this block of code:

 if 'setup' in item.fixturenames:
                        web_driver = item.funcargs['setup']
                    else:
                        print('Fail to take screenshot.No setup fixture found')

Here is conftest.py:

  @pytest.fixture(scope='function')
def get_webdriver(get_edge_options):
    options = get_edge_options
    print("Current working dir : %s" % os.getcwd())
    s = Service('D:\mmanager\msedgedriver.exe')
    driver = webdriver.Edge(service=s, options=options)
    # driver.delete_all_cookies()
    return driver


@pytest.fixture(scope='function')  # function means run each test in new browser session
def setup(request, get_webdriver):
    driver = get_webdriver
    if request.cls is not None:
        request.cls.driver = driver
    driver.get(FCC_HOME)
    yield driver
    driver.quit()


# Shared Given Steps

@given('the MM3-0 login page is displayed', target_fixture='MM_Login_page')
def MM_Login_page(setup):
    pass



@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if (rep.when == 'call' or rep.when == 'setup') and (rep.failed or rep.skipped):
        try:
            if 'setup' in item.fixturenames:
                web_driver = item.funcargs['setup']
            else:
                print('Fail to take screenshot.No setup fixture found')
                return
            allure.attach(
                web_driver.get_screenshot_as_png(),
                name='!! Screenshot Captured !!',
                attachment_type=allure.attachment_type.PNG)
        except Exception as e:
            print('Fail to take screen-shot: {}'.format(e))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文