What if you could run many test setups with just one simple test function?
Why Parametrize with indirect fixtures in PyTest? - Purpose & Use Cases
Imagine you have a test that needs to run with different setups, like different user accounts or database states. You try to write separate tests for each setup manually.
This manual way means copying and pasting similar code many times. It is slow to write, hard to update, and easy to make mistakes. You might forget to change something or run the wrong setup.
Using parametrize with indirect fixtures lets you write one test that automatically runs with many setups. The fixture prepares the environment based on parameters, so your test stays clean and easy to manage.
def test_user1(): setup_user('user1') assert do_something() def test_user2(): setup_user('user2') assert do_something()
@pytest.mark.parametrize('user', ['user1', 'user2'], indirect=True) def test_users(user): assert do_something()
You can run many test scenarios automatically with less code and fewer mistakes.
Testing a website login with different user roles without writing separate tests for each role.
Manual test duplication wastes time and causes errors.
Parametrize with indirect fixtures runs one test many ways cleanly.
This makes tests easier to write, read, and maintain.