Chain Fixtures Requests

Introduction to Testing in Python

Alexander Levin

Data Scientist

What is a chain request

  • Chain fixtures requests - a pytest feature, that allows a fixture to use another fixture
  • Creates a composition of fixtures

fixture calls other fixture

Introduction to Testing in Python

Why and when to use

Chain fixtures requests help to:

  • Establish dependencies between fixtures
  • Keep the code modular

When it can be useful:

  • When we have several fixtures that depend on each other
Introduction to Testing in Python

Example of chain requests

# Fixture that is requested by the other fixture
@pytest.fixture
def setup_data():
    return "I am a fixture!"
# Fixture that is requested by the test function
@pytest.fixture
def process_data(setup_data):
    return setup_data.upper()
# The test function
def test_process_data(process_data):
    assert process_data == "I AM A FIXTURE!"
Introduction to Testing in Python

How to use chain requests

  1. Prepare the program we want to test
  2. Prepare the testing functions
  3. Prepare the pytest fixtures
  4. Pass the fixture name to the other fixture signature
# Fixture requesting other fixture
@pytest.fixture
def process_data(setup_data):
    return setup_data.upper()
Introduction to Testing in Python

Summary

  • Chain fixture requests - is a feature that allows a fixture to use another fixture (creating fixture compositions)
  • It helps to divide the code by functions and keep it modular
  • Example use case: the steps of data pipeline
  • To use chain fixture requests pass the fixture name to the other fixture signature
Introduction to Testing in Python

Let's practice!

Introduction to Testing in Python

Preparing Video For Download...