I am using fixtures as a way to set up/tear down data for E2E tests. Could be API calls or directly interacting with a page that isn’t explicitly part of the test, but needs to happen to run test steps.
Most of my fixtures just return a function call that I can use in the test to pass in different arguments. The reason I’m not just using helper functions is that the teardown is needed to delete any created data during the test, and needs to be tightly coupled to the creation so as to only delete what was created by this specific test. Also, teardown must happen after ever test regardless of pass or fail, so I can’t just put a custom teardown function at the bottom of the test and call it good.
currently my fixtures look something like this:
@pytest.fixture
def create_data(possible_other_fixtures):
def callable_func(param_1, param_2):
val = other_function(param_1, param_2)
return val
val = None
yield callable_func
delete_data(val)
And then in the test is called like this:
def test_something(create_data):
# Setup
val = create_data(arg_1, arg_2)
# rest of test
...
It would be nice to not have to worry about the inner/outer nature of the fixtures and use them just like a regular function that has an auto teardown
I know that you can use @pytest.mark.parametrize, but I don’t like passing all of the params at the top, and it is all around too black boxy for my taste. Additionally, sometimes the values passed into the fixture are outputs from a previous fixture or function call, so they are not always known.
In the end what I have works, but I’m just trying to find out if there are any better alternatives.
btmarchant is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.