Conftest fixtures let you share setup code across many test files easily. This helps avoid repeating the same code in each test.
Conftest fixtures (shared across files) in PyTest
In conftest.py file: import pytest @pytest.fixture def my_fixture(): # setup code resource = None # define resource before yield yield resource # teardown code
The conftest.py file should be placed in the test directory or a parent directory.
Fixtures defined here are automatically available to all test files in the same directory or subdirectories.
import pytest @pytest.fixture def sample_data(): return [1, 2, 3]
import pytest @pytest.fixture def resource(): print('Setup resource') yield 'resource ready' print('Teardown resource')
The fixture greeting is defined once in conftest.py. The test in test_example.py uses it without importing. It asserts the value and prints it.
### conftest.py ### import pytest @pytest.fixture def greeting(): return 'Hello from conftest' ### test_example.py ### def test_use_greeting(greeting): assert greeting == 'Hello from conftest' print(greeting)
Do not import conftest.py in your test files; pytest finds it automatically.
Keep conftest.py clean and focused on fixtures to avoid confusion.
Conftest fixtures let you share setup code across multiple test files.
Place fixtures in conftest.py in your test folder or above.
Tests can use these fixtures automatically without imports.
Practice
conftest.py file in pytest?Solution
Step 1: Understand the role of
Theconftest.pyconftest.pyfile is used to define fixtures that can be shared across multiple test files without needing explicit imports.Step 2: Identify the purpose of sharing setup code
Sharing setup code helps avoid repetition and makes tests cleaner and easier to maintain.Final Answer:
To share setup code automatically across multiple test files without importing -> Option BQuick Check:
conftest.py shares fixtures automatically [OK]
- Thinking conftest.py runs tests in parallel
- Believing conftest.py stores test data
- Assuming fixtures in conftest.py are for one file only
conftest.py for sharing across test files?Solution
Step 1: Recall fixture syntax in pytest
Fixtures must be decorated with@pytest.fixtureto be recognized by pytest.Step 2: Check each option
@pytest.fixture def my_fixture(): return 42 correctly uses the decorator and defines a function returning a value. Other options miss the decorator or use wrong syntax.Final Answer:
@pytest.fixture\ndef my_fixture(): return 42 -> Option CQuick Check:
Fixtures need @pytest.fixture decorator [OK]
- Forgetting the @pytest.fixture decorator
- Defining fixtures as classes without decorator
- Assigning fixture values without function
conftest.py fixture and test file, what will be the output when running pytest?
-- conftest.py --
import pytest
@pytest.fixture
def number():
return 5
-- test_sample.py --
def test_double(number):
assert number * 2 == 10
def test_triple(number):
assert number * 3 == 15
Solution
Step 1: Understand fixture usage
The fixturenumberreturns 5 and is shared automatically viaconftest.py.Step 2: Evaluate test assertions
test_double checks 5*2 == 10 (True), test_triple checks 5*3 == 15 (True). Both assertions are correct.Final Answer:
Both tests pass -> Option AQuick Check:
Fixture returns 5, assertions match [OK]
- Assuming fixture is not shared without import
- Miscalculating expected assertion values
- Confusing test pass/fail logic
conftest.py as follows:
import pytest
@pytest.fixture
def data():
return [1, 2, 3]
In your test file, you wrote:
def test_sum(data):
assert sum(data) == 10
What is the problem and how to fix it?Solution
Step 1: Check the fixture return value
The fixturedatareturns the list [1, 2, 3].Step 2: Calculate the sum and compare with assertion
sum([1, 2, 3]) equals 6, but the test asserts it equals 10, which is incorrect.Final Answer:
Assertion is wrong; sum([1,2,3]) is 6, so change 10 to 6 -> Option AQuick Check:
sum([1,2,3]) = 6, fix assertion [OK]
- Thinking fixture must be imported explicitly
- Assuming sum() can't handle lists
- Changing fixture type unnecessarily
conftest.py:
import pytest
@pytest.fixture(scope="session")
def db_connection():
conn = create_connection()
yield conn
conn.close()
What is the best reason to use scope="session" here?Solution
Step 1: Understand fixture scopes in pytest
Scope "session" means the fixture is created once for the entire test run and shared across all tests.Step 2: Apply to database connection use case
Creating the connection once saves time and resources, and closing it after all tests is efficient.Final Answer:
To create the connection once per test session and reuse it in all tests -> Option DQuick Check:
Session scope = one setup per test run [OK]
- Thinking session scope creates new instance per test
- Assuming connection closes before each test
- Confusing scope with parallel test execution
Ready to prove it?
Pass the test and this pattern moves unknown
