How to pass fixture object to pytest_runtest_makereport?

We use the pytest library for automation test. 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. How can I do this?

Additional: We don't use the conftest.py file. Can we use the pytest_runtest_makereport method without conftest.py? We use just a sampleTest.py file.

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(' 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 

1 Answer

You can access fixtures in 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:


Use pytest_runtest_makereport without 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) 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like