Discover how one simple file can save you hours of repetitive test setup!
Why Conftest.py purpose in PyTest? - Purpose & Use Cases
Imagine you have many test files, and each one needs the same setup steps like opening a browser or preparing test data.
You write these steps again and again inside every test file.
This manual way is slow and boring.
You might forget to update one test file when the setup changes.
It causes mistakes and wastes time.
Conftest.py lets you write setup code once and share it with all your tests automatically.
This keeps your tests clean and easy to maintain.
def test_one(): setup() assert something def test_two(): setup() assert something_else
import pytest @pytest.fixture def setup(): # setup code pass # tests just use the fixture without repeating setup
It enables writing DRY (Don't Repeat Yourself) tests that are easier to read and faster to update.
When testing a website, you can open the browser once in conftest.py and all tests will use it without repeating the code.
Conftest.py centralizes shared test setup.
It reduces repeated code and errors.
It makes tests cleaner and easier to maintain.
Practice
conftest.py in pytest?Solution
Step 1: Understand the role of
conftest.pyconftest.pyis used to hold fixtures and setup code that multiple test files can use.Step 2: Compare with other options
Options A, B, and C describe other pytest features or tools but not the purpose ofconftest.py.Final Answer:
To share setup code and fixtures across multiple test files -> Option CQuick Check:
Sharing fixtures = D [OK]
- Thinking conftest.py runs tests
- Confusing it with test data files
- Assuming it generates reports
conftest.py?Solution
Step 1: Recall fixture syntax in pytest
Fixtures are defined using the@pytest.fixturedecorator followed by a function.Step 2: Check each option
@pytest.fixture def my_fixture(): return 42 correctly uses@pytest.fixtureand defines a function returning a value. Others either lack the decorator or are incorrect syntax.Final Answer:
@pytest.fixture\ndef my_fixture(): return 42 -> Option BQuick Check:
Fixture needs @pytest.fixture decorator = A [OK]
- Defining fixtures without @pytest.fixture
- Using classes instead of functions
- Assigning values without a function
conftest.py fixture:
import pytest
@pytest.fixture
def number():
return 5
And this test file:
def test_double(number):
assert number * 2 == 10
What will be the test result when running pytest?Solution
Step 1: Understand fixture usage
The fixturenumberreturns 5 and is automatically injected into the test function.Step 2: Evaluate the assertion
The test assertsnumber * 2 == 10, which is5 * 2 == 10, true, so the test passes.Final Answer:
Test passes successfully -> Option AQuick Check:
Fixture value 5 * 2 = 10 passes [OK]
- Assuming fixture is not found causing error
- Thinking test fails because of wrong value
- Confusing fixture with test parameter
conftest.py fixture:
import pytest
@pytest.fixture
def data():
return [1, 2, 3]
But your test fails with an error: fixture 'data' not found. What is the likely cause?Solution
Step 1: Understand pytest fixture discovery
Pytest findsconftest.pyfiles in the test directory or its parents. Ifconftest.pyis elsewhere, fixtures won't be found.Step 2: Analyze other options
Options A, B, and D would cause different errors or test failures, but not 'fixture not found'.Final Answer:
Theconftest.pyfile is not in the same or parent directory of the test file -> Option DQuick Check:
Fixture not found means wrong file location = C [OK]
- Ignoring file placement rules
- Assuming missing return causes fixture not found
- Not passing fixture as test parameter
conftest.py. Which approach best follows pytest best practices?Solution
Step 1: Understand fixture scope and cleanup
Using a fixture withyieldallows setup before tests and cleanup after, ideal for resources like DB connections.Step 2: Evaluate other options
Write the connection setup code inside each test function separately repeats code, violating DRY. Create a global variable inconftest.pyholding the connection without a fixture lacks cleanup and is not recommended. Use a class with static methods inconftest.pyto manage the connection is more complex and less idiomatic.Final Answer:
Define a fixture inconftest.pythat creates and yields the connection, then closes it after tests -> Option AQuick Check:
Fixture with yield for setup/teardown = A [OK]
- Duplicating setup in tests
- Not cleaning up resources
- Using global variables instead of fixtures
Ready to prove it?
Pass the test and this pattern moves unknown
