What if you could fix a setup bug once and have it fixed everywhere instantly?
Why Conftest fixtures (shared across files) in PyTest? - Purpose & Use Cases
Imagine you have many test files, each needing the same setup steps like creating a user or connecting to a database. You copy-paste this setup code into every test file.
This manual way is slow and risky. If you change the setup, you must update every file. It's easy to forget one, causing inconsistent tests and wasted time fixing bugs.
Conftest fixtures let you write the setup code once in a shared file. All test files can use it automatically. This keeps tests clean, consistent, and easy to maintain.
def test_one(): user = create_user() assert user.is_active def test_two(): user = create_user() assert user.name == 'test'
# conftest.py import pytest @pytest.fixture def user(): return create_user() # test_one.py def test_one(user): assert user.is_active # test_two.py def test_two(user): assert user.name == 'test'
You can share setup code easily across many tests, making your test suite faster to write and simpler to update.
In a big project, you need to log in before many tests. Using conftest fixtures, you write the login code once. Every test file uses it without repeating code.
Manual setup code duplication wastes time and causes errors.
Conftest fixtures share setup code across test files automatically.
This makes tests cleaner, consistent, and easier to maintain.
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
