Manual fixtures (using dependency injection)
If we don’t set the autouse to True then by default it is False. Meaning you will have to explicitly use them. However instead of calling these function we use their names as parameters of our test functions. Pytest will notice that your test function requires certain parameters, it will find the fixture with the given name, call it, and set the variable to the value “returned” by the fixture.
import pytest
@pytest.fixture()
def blue():
print("Blue setup")
yield
print("Blue teardown")
@pytest.fixture()
def green():
print("Green setup")
yield
print("Green teardown")
#def test_try(yellow):
# print("yellow")
def test_one(blue, green):
print(" Test one")
def test_two(green, blue):
print(" Test two")
assert False
Output:
Blue setup
Green setup
Test one
Green teardown
Blue teardown
Green setup
Blue setup
Test two
Blue teardown
Green teardown
- We can’t add fixtures to test_functions as decorators (as I was the case in NoseTest), we need to use dependency injection.