How to make a setup function with Pytest

To make a setup function, Pytest provides fixture functions. A fixture function is a simple function that is launched before every test functions in the test class. This method allows to make a common setup function for all the tests in the file/class, which can be very useful. It can also be a function which is launched before a single test, outside of a class. Here is an example :


def multiply(x, y):
    return x * y

@pytest.fixture
def setup():

    # SETUP

    lst = []
    for i in range(10):
        lst.append((i, i*2, i*i*2))

    yield lst

    # TEARDOWN

    lst = []

def test_multiply(setup):                           #setup = lst
    for i in setup:
        assert multiply(i[0],i[1]) == i[2]

def test_reverse_lst(setup):
    setup.reverse()
    assert setup[0][2] == 162    #(9*9*2)

def test_lst(setup):
    assert setup[0][2] == 0      #(0*0*2)

The first method is multiplying x by y. Then the test_multiply function has setup in his parameter. It means that the test function is calling the fixture specified by @pytest.fixture. The function named setup is launched just before test_multiply to load the data we need and return it with yield. It could have been return but return doesn’t allow us to continue the function after it is called. When pytest reach yield, it gives the data to test_multiply and the test can go forward. Once it is finished, the setup function continues after the yield, it becomes now a teardown function.

A fixture allows us to setup some data before a test function process and after it has done its work. After the yield, we can free some memory, or close an application, etc…

To go Further

Want to contribute? Check those guidelines.