smarie / python-pytest-steps

A tiny package to ease the creation of test steps with shared intermediate results/state.
https://smarie.github.io/python-pytest-steps/
BSD 3-Clause "New" or "Revised" License
56 stars 5 forks source link

pytest-harvest + pytest-steps: results_bag fixture is cross-step, how to get per-step? #49

Closed j-carson closed 2 years ago

j-carson commented 3 years ago

I have just started using your pytest extensions - they are wonderful, thank you!

I had a pytest-steps test with four steps, and each step saved the current value of a variable at the end of each step. The results_bag only had the value of the variable at the last step, and that value appeared in the row for the first step.

I fixed it by creating a per-step results bag fixture and using that in place of results_bag as the argument to my test. Is this the best way to get my desired behavior?


@pytest.fixture
@one_fixture_per_step
def step_bag(request):
    """pytest-harvest creates a results_bag per step so that it
    can calculate duration_ms per step, but results_bag seems
    to not be updated per step in my tests. This fixture
    explicitly grabs the current step's bag.

    Parameters
    ----------
    request: current running test object
        Fixture provided by pytest
    """
    return request.getfixturevalue("results_bag")
smarie commented 3 years ago

Thanks @j-carson ! This is a very nice way indeed to fix this issue. However if results_bag is not explicitly declared as a test dependency in your test function, request.getfixturevalue("results_bag") might not work.

Therefore, I would suggest the following, I suspect that it works too:

@pytest.fixture
@one_fixture_per_step
def step_bag(results_bag):
    return results_bag

Do you confirm ?

More generally I think that your proposal could make a great addition to pytest-steps. However it should be optional: if this fixture is provided by the plugin, and the user does not use it in any of its tests, the user should not need pytest-harvest to be installed. I think that with the above design, as long as "auto use" is not on, this is the pytest default behaviour. Do you confirm ?

j-carson commented 3 years ago

The shorter implementation of step_bag fixture works for me.

I think that with the above design, as long as "auto use" is not on, this is the pytest default behaviour. Do you confirm ?

I will need to write a test case to verify this second question.

j-carson commented 3 years ago

Wrote the test case for above: In a virtualenv with pytest-steps installed but not pytest-harvest, the above fixture will not cause an error if that fixture is not used by any test.

smarie commented 3 years ago

Cool ! Then it seems that all lights are green :) Would you like to propose a PR then ?

The fixture would need to live in module plugin.py and have at least one dedicated test. If you feel like updating the documentation markdown accordingly

(No need to test the case when the pytest-harvest plugin is not installed, this is a pytest mechanism and it wont change over time).

let me know !

smarie commented 2 years ago

Fixed by #46