One of the great things about PyTest is that it is very flexible.

It can reduce complex testing requirements to simpler and organized functions that can then depend on other functions according to their needs.

Fixtures The ability to invoke other fixtures is a measure of flexibility.

A: Fixtures calls the other Fixtures

Take a quick look at a simple example:

import pytest # Arrange @pytest.fixture def first_entry(): Fixture def order(first_entry) # Arrange = pytest.fixture def order(first_entry): This is another fixture function that requested the previous fixture function, first_entry(), and put the return value of first_entry() in the list []. Return [first_entry] def test_string(order): [] order.append("b") # Assert Assert order == ["a", "b"]Copy the code

As you can see, a fixture in PyTest requests other fixtures, just as a test function requests fixtures, and all the request rules apply. Again, if we were to do these things ourselves, it would look something like this:

def first_entry():
    return "a"


def order(first_entry):
    return [first_entry]


def test_string(order):
    # Act
    order.append("b")

    # Assert
    assert order == ["a", "b"]


entry = first_entry()
the_list = order(first_entry=entry)
test_string(order=the_list)
Copy the code

Fixtures: a measure of reusability

Fixtures in PyTest also fixtures allows us to define generic setup steps that will be reused over and over again as we do with normal functions.

Two different test functions can request the same fixture, and each test function gets its own result for that fixture.

This has the advantage of ensuring that the different test functions do not interact with each other. We can use this mechanism to ensure that each test function gets its own new, clean, consistent data.

import pytest


# Arrange
@pytest.fixture
def first_entry():
    return "a"


# Arrange
@pytest.fixture
def order(first_entry):
    return [first_entry]


def test_string(order):
    # Act
    order.append("b")

    # Assert
    assert order == ["a", "b"]


def test_int(order):
    # Act
    order.append(2)

    # Assert
    assert order == ["a", 2]
Copy the code

As you can see from the code, the fixture function order is called by two test functions, but it gives the same result each time. This does not affect the return value of order in test_int because order.append(“b”) is used in test_string.

Similarly, these things for ourselves to do, that is, like this:

def first_entry():
    return "a"


def order(first_entry):
    return [first_entry]


def test_string(order):
    # Act
    order.append("b")

    # Assert
    assert order == ["a", "b"]


def test_int(order):
    # Act
    order.append(2)

    # Assert
    assert order == ["a", 2]


entry = first_entry()
the_list = order(first_entry=entry)
test_string(order=the_list)

entry = first_entry()
the_list = order(first_entry=entry)
test_int(order=the_list)
Copy the code

Fixtures are then continued with the official documentation: multiple fixtures are required at one time, fixtures are required multiple times.