Fixtures

One of the great things about pytest is how it facilitates creating reusable features so that we can feed our tests with data or objects in order to test more effectively and without repetition.

For example, we might want to create a MergeRequest object in a particular state, and use that object in multiple tests. We define our object as a fixture by creating a function and applying the @pytest.fixture decorator. The tests that want to use that fixture will have to have a parameter with the same name as the function that's defined, and pytest will make sure that it's provided:

@pytest.fixturedef rejected_mr():    merge_request = MergeRequest()    merge_request.downvote("dev1")    merge_request.upvote("dev2")    merge_request.upvote("dev3")

Get Clean Code in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.