In the examples I’ve used so far in this series , tests only are using at most one named fixture.
You can use more.
Simple example:
import pytest
@pytest.fixture(scope="module")
def foo():
print('\nfoo setup - module fixture')
yield
print('foo teardown - module fixture')
@pytest.fixture()
def bar():
print('bar setup - function fixture')
yield
print('bar teardown - function fixture')
@pytest.fixture()
def baz():
print('baz setup - function fixture')
yield
print('baz teardown - function fixture')
def test_one(foo, bar, baz):
print('in test_one()')
def test_two(foo, bar, baz):
print('in test_two()')
$ pytest -s test_multiple.py
================== test session starts ==================
collected 2 items
test_multiple.py
foo setup - module fixture
bar setup - function fixture
baz setup - function fixture
in test_one()
.baz teardown - function fixture
bar teardown - function fixture
bar setup - function fixture
baz setup - function fixture
in test_two()
.baz teardown - function fixture
bar teardown - function fixture
foo teardown - module fixture
=================== 2 passed in 0.06s ===================
This post is part of a series on pytest fixtures
- pytest fixtures nuts and bolts
- pytest xunit-style fixtures
- Basic pytest fixtures example
- Using pytest fixtures by naming them
- Using pytest autouse fixtures
- Using pytest fixtures with mark.usefixtures
- pytest fixture return value
- pytest fixture teardown
- pytest fixture scope
- Parametrizing pytest fixtures with param
- Using multiple pytest fixtures - this post
- Modularity: pytest fixtures using other fixtures
- pytest session scoped fixtures
- Mixing pytest fixture scope)