I have a test that makes a thing, validates it, deletes the thing and confirms it was deleted.
def test_thing():
thing = Thing() # Simplified, it actually takes many lines to make a thing
assert thing.exists
thing.delete() # Simplified, it also takes a few lines to delete it
assert thing.deleted
Next I want to make many more tests that all use the thing, so it's a natural next step to move the thing creation/deletion into a fixture
@pytest.fixture
def thing():
thing = Thing() # Simplified, it actually takes many lines to make a thing
yield thing
thing.delete() # Simplified, it also takes a few lines to delete it
def test_thing(thing):
assert thing.exists
def test_thing_again(thing):
# Do more stuff with thing
...
But now I've lost my assert thing.deleted.
I feel like I have a few options here, but none are satisfying.
- I could assert in the fixture but AFAIK it's bad practice to put assertions in the fixture because if it fails it will result in an
ERRORinstead of aFAIL. - I could keep my original test and also create the fixture, but this would result in a lot of duplicated code for creating/deleting the thing.
- I can't call the fixture directly because I get a
Fixture called directlyexception so I could move the thing creation out into a generator that is used by both the fixture and the test. This feels clunky though and what happens if mythingfixture needs to use another fixture?
What is my best option here? Is there something I haven't thought of?