Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixtures cannot be retrieved in pytest_runtest_makereport function for tests that are skipped during setup with the pytest.mark.skip marker #13101

Closed
harmin-parra opened this issue Jan 2, 2025 · 9 comments
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity

Comments

@harmin-parra
Copy link

harmin-parra commented Jan 2, 2025

I am trying to retrieve the fixtures when a test has been skipped with the skip marker.

But this is impossible because pytest doesn't pass the fixture of skipped tests during setup to the pytest_runtest_makereport function

Steps to reproduce:

  1. have a custom-made fixture called my_fixture in conftest.py file
@pytest.fixture(scope='session')
def my_fixture(request):
    pass
  1. write the following test:
@pytest.mark.skip
def test_skipped(my_fixture):
    pass
  1. Add this function to the conftest.py file
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    if call.when == 'setup':
        if report.skipped:
            feature_request = item.funcargs['request']
            my_fixture = feature_request.getfixturevalue("my_fixture")
            # Do some stuff here

This piece of code doesn't work because item.funcargs = {} and item.funcargs['request'] will raise an exception.
pytest doesn't retrieve the fixtures of skipped tests during setup. Why?

Tested with pytest 8.3.4

@harmin-parra harmin-parra changed the title fixtures cannot be retrieved in pytest_runtest_makereport function for tests that are skipped with the pytest.mark.skip marker fixtures cannot be retrieved in pytest_runtest_makereport function for tests that are skipped during setup with the pytest.mark.skip marker Jan 2, 2025
@RonnyPfannschmidt RonnyPfannschmidt added type: question general question, might be closed after 2 weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly labels Jan 2, 2025
@RonnyPfannschmidt
Copy link
Member

skipped tests don't get their fixtures filled

thats intended behavior

@RonnyPfannschmidt RonnyPfannschmidt added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Jan 3, 2025
Copy link
Contributor

This issue is stale because it has the status: needs information label and requested follow-up information was not provided for 14 days.

@github-actions github-actions bot added the stale label Jan 18, 2025
@harmin-parra
Copy link
Author

I would say that skipped tests during setup should get their fixtures.

Why did you make an exception with skipped tests during setup?

@nicoddemus
Copy link
Member

I would say that skipped tests during setup should get their fixtures.

If they will not run at all, then it makes little sense to do their setup and teardown phase. Why do you think that they should get filled, can you elaborate?

@harmin-parra
Copy link
Author

harmin-parra commented Jan 20, 2025

Sure, this is my use case.

I want to recover the reason that was passed as argument to the skip marker
@pytest.mark.skip(reason="xxxx")

I want to parse the reason in order to detect a predefined issue pattern and add a link to the report.
Something similar as it is done in Allure.

I want to define an issue pattern like PROJ-123 resulting in a link like http://jira.atlassian.com/PROJ-123

So, this is why I want to read the fixture to see if I detect the pattern in the reason (ex: @pytest.mark.skip(reason="PROJ-123")) and add the link to the report.

This is why I also want to do it with skipped tests during setup.
It is in my fixture where I store the issue pattern. This is why I need to get the fixture.

@github-actions github-actions bot removed the stale label Jan 21, 2025
@RonnyPfannschmidt
Copy link
Member

The easiest way to do this is to use an own marker and call skip in an autouse fixture

@harmin-parra
Copy link
Author

harmin-parra commented Jan 21, 2025

But still, with custom-made markers, you need to use fixtures.
I don't think you can get away with fixture when using autouse fixture with custom-made markers

@harmin-parra
Copy link
Author

or maybe I can get the marker like this :
value = item.iter_markers(name="my_marker")
in the pytest_runtest_makereport hooker ?

@nicoddemus
Copy link
Member

nicoddemus commented Jan 22, 2025

@RonnyPfannschmidt is suggesting something like:

@pytest.fixture(autouse=True)
def check_skip_because(request: pytest.FixtureRequest) -> None:
    if marker := request.node.get_closest_marker("skip_because"):
        reason = marker.args[0]
        # Parse link from `reason` and add to report.
        pytest.skip(f"Skipped due to {reason}")       

And use like this:

@pytest.mark.skip_because("See PROJ-123")
def test_skipped(my_fixture):
    pass

IOW, you do not use the builtin skip marker, which is implemented differently and skips setup, and instead skip imperatively during the test setup phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants