Fixtures is used to prepare a test, so how does PyTest know which test function or fixtures to use? Definition: a measure of fixtures.

Test function declarations pass parameters to request fixtures

Test functions request fixtures by declaring fixtures as parameters.

Def test_my_fruit_in_basket(my_fruit, fruit_basket): this is a test function to assert my_fruit in fruit_basketCopy the code

Referring to the example in the previous chapter, the test function test_my_fruit_in_basket calls both fixtures by passing in my_fruit, fruit_basket.

When PyTest runs a test function, it looks at the parameters in that test function and searches for fixtures with the same name as those parameters. Once PyTest finds those objects, it runs those fixtures.

Second, return values in fixtures are passed to test functions

In addition, if there is anything returned from the fixture, PyTest takes it and passes it as an argument to the test function. Here’s an example:

class Fruit: def __init__(self, name): self.name = name self.cubed = False def cube(self): self.cubed = True class FruitSalad: def __init__(self, *fruit_bowl): self.fruit = fruit_bowl self._cube_fruit() def _cube_fruit(self): for fruit in self.fruit: fruit.cube() # Arrange @pytest.fixture def fruit_bowl(): return [Fruit("apple"), Fruit("banana")] def test_fruit_salad(fruit_bowl): Fruit_bowl [Fruit("apple"), Fruit("banana")]], fruit_bowl [Fruit("apple"), Fruit("banana")]], fruit_bowl Use fruit_salad = FruitSalad(*fruit_bowl) # Assert # all() to determine whether all elements in the given iterable are TRUE. Assert all(fruit.cubed for fruit in fruit_salad. Fruit)Copy the code

Ps: You could write a few lines of very simple code here, but suddenly it doesn’t hurt to look at a little bit of convoluted code.

For those of you who are not familiar with Python, the official example may seem a bit opaque, but that’s not what we’re focusing on, and we’re trying to figure out the transitive relationship:

  1. First, test the functiontest_fruit_salad requestfruit_bowl(i.e.def test_fruit_salad(fruit_bowl):)
  2. At this point, PyTest will execute the fixture functionfruit_bowlAnd treat the returned object asfruit_bowlParameters are passed to the test functiontest_fruit_salad.

That’s what happens when a fixture is called on request.

If the fixture function above did something we’d do ourselves manually, it would look like this:

# change the two classes above... def fruit_bowl(): return [Fruit("apple"), Fruit("banana")] def test_fruit_salad(fruit_bowl): # Act fruit_salad = FruitSalad(*fruit_bowl) # Assert assert all(fruit.cubed for fruit in fruit_salad.fruit) # Arrange bowl = fruit_bowl() test_fruit_salad(fruit_bowl=bowl)Copy the code

You should already be familiar with how fixtures are called.

If you find the official code example a bit cryptic, here’s a simpler version:

Import pytest # Arrange @pytest.fixture def fruit_bowl(): return [" apple ", "banana "] def test_fruit_salad(): # fruit_salad == fruit_bowl[0] + fruit_bowl[1]Copy the code

Next, follow the official documentation to understand the characteristics of fixtures: fixtures call other fixtures, and the reusability of fixtures.