Skip to content

Retrieve a value after the yield statement from a fixture #101

@TheoCrd

Description

@TheoCrd

I have a fixture

@pytest.fixture(scope="session", autouse=True)
def existing_crash_dumps(request):
    request.session.crash = False
    existing_crash_dumps = list_crash_dumps()
    print(f"Existing crash dumps before tests: {existing_crash_dumps}")

    yield existing_crash_dumps

    if is_new_dump(existing_crash_dumps):
        request.session.crash = True

Here, I want to retrieve the crash value and put it in my report

def pytest_json_runtest_metadata(item, call):

    if call.when != "call":
        
        return {}
  
    crash = getattr(item, "crash")

    metadata = {
        "date": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "crash": crash
    }

    return metadata

But the value is always False, even when I entered the if statement after yield. I understood that after yield, we are in the teardown phase of a test, but is there a way to retrieve a value from this point? Basically, what's after the yield statement is what I want to check after a test has finished, hence checking at teardown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions