The easiest way to use a fixture is to name it.
We can put the fixture name in the parameter list for each test that needs it:
import pytest
@pytest.fixture()
def before():
print('\nbefore each test')
def test_1(before):
print('test_1()')
def test_2(before):
print('test_2()')
The fixture is going to be called for every test that names the fixture in it’s parameter list.
Output:
$ pytest -s test_fixture.py
=========== test session starts ============
collected 2 items
test_fixture.py
before each test
test_1()
.
before each test
test_2()
.
============ 2 passed in 0.05s =============
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 - This post
- 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
- Modularity: pytest fixtures using other fixtures
- pytest session scoped fixtures
- Mixing pytest fixture scope)