1

Here, I have integrated framework like (API + UI) in single framework using pytest and I want to use below hook with pytest framework:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call)

but when I'm using above hook in conftest.py file, it's launching web browser and saving screenshots for ui and api both types of testcase but here my requirement is that I only want browser should be launch and save screenshots for ui and not for api testcases .

Requirement:

is there any way in pytest framework or generally if we can customized browser launching and failure screenshots capturing for UI testcases only and not for api testcases. ?

1 Answers1

1

You can do it by explicitly marking your tests and saving screenshots only for tests with UI mark:

pytest.ini

[pytest]
markers =
    api: API tests
    ui: UI tests

conftest.py

def pytest_runtest_makereport(item, call):
    if call.when == 'call' and call.excinfo is not None:
        if any(mark.name == 'ui' for mark in item.own_markers):
            # save screenshot here
            ...

test_example.py

import pytest


@pytest.mark.api
def test_api():
    assert False


@pytest.mark.ui
def test_ui():
    assert False
Andrei Evtikheev
  • 331
  • 2
  • 12
  • Hey @andrei thanks for you solution but It is still causing me the same problem when I'm running my test for api testcases because we are saving screenshots in conftest.py file and to save the screenshot we need to write something like driver.save_screenshot(). so here we are referring driver instance and it is also launching the driver instance in case of api testcase when importing driver like from helpers.driver_manage import driver – Satyam Agrawal Jun 08 '23 at 05:00