Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Manual fixtures (dependency injection)

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.