August 2018
Beginner
160 pages
4h 8m
English
Pytest's solution to this problem is fixtures. Fixtures are used to provide resources that test the functions and methods we need to execute.
They are created using normal Python functions and the @pytest.fixture decorator:
@pytest.fixturedef comedy_series(): return [ ("The Office", 2005, 8.8), ("Scrubs", 2001, 8.4), ("IT Crowd", 2006, 8.5), ("Parks and Recreation", 2009, 8.6), ("Seinfeld", 1989, 8.9), ]
Here, we are creating a fixture named comedy_series, which returns the same list we were using in the previous section.
Tests can access fixtures by declaring the fixture name in their parameter list. The test function then receives the return value of the fixture function as a parameter. Here is the comedy_series fixture ...
Read now
Unlock full access