A conftest.py file is a special configuration file in pytest used to define fixtures that can be shared across multiple test files in the same directory or subdirectories.
Click to reveal answer
beginner
How does pytest find fixtures defined in conftest.py?
Pytest automatically discovers fixtures in <code>conftest.py</code> files by looking in the test directory and its parent directories without needing to import them explicitly.
Click to reveal answer
beginner
Why use conftest.py fixtures instead of defining fixtures in each test file?
Using conftest.py fixtures helps avoid repeating fixture code in multiple test files, making tests cleaner and easier to maintain by sharing setup logic.
Click to reveal answer
intermediate
Can fixtures in conftest.py have different scopes? Give an example.
Yes, fixtures in conftest.py can have scopes like function, module, or session. For example, a session scoped fixture runs once per test session and can be shared across all tests.
Click to reveal answer
beginner
How do you use a fixture defined in conftest.py inside a test function?
Simply add the fixture name as a parameter to the test function. Pytest will inject the fixture automatically without needing to import it.
Click to reveal answer
What is the main purpose of conftest.py in pytest?
ATo share fixtures across multiple test files
BTo store test data files
CTo configure pytest command line options only
DTo write test cases
✗ Incorrect
The conftest.py file is mainly used to define fixtures that can be shared across multiple test files.
How do you access a fixture defined in conftest.py inside a test?
AFixtures in <code>conftest.py</code> cannot be accessed in tests
BImport the fixture explicitly in the test file
CCall the fixture function manually inside the test
DAdd the fixture name as a parameter to the test function
✗ Incorrect
Pytest injects fixtures automatically when you add the fixture name as a parameter to the test function.
Which of these scopes can a fixture in conftest.py have?
Afunction
BAll of the above
Csession
Dmodule
✗ Incorrect
Fixtures can have scopes like function, module, or session to control how often they run.
If you have multiple conftest.py files in nested directories, which fixtures are available to tests?
AFixtures from all <code>conftest.py</code> files in parent directories
BFixtures are not shared across directories
CFixtures only from the root <code>conftest.py</code>
DOnly fixtures in the closest <code>conftest.py</code>
✗ Incorrect
Pytest searches up the directory tree and makes fixtures from all conftest.py files in parent directories available.
What happens if two conftest.py files define fixtures with the same name?
APytest raises an error
BBoth fixtures run in order
CThe fixture in the closest <code>conftest.py</code> overrides the others
DFixtures are merged automatically
✗ Incorrect
The fixture defined in the closest conftest.py to the test file overrides fixtures with the same name in parent directories.
Explain how conftest.py helps in sharing fixtures across multiple test files in pytest.
Think about how pytest finds and uses fixtures without explicit imports.
You got /4 concepts.
Describe the different fixture scopes you can use in conftest.py and why you might choose each.
Consider how often you want the setup to run to save time or reset state.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of placing fixtures in a conftest.py file in pytest?
easy
A. To run tests in parallel automatically
B. To share setup code automatically across multiple test files without importing
C. To store test data in a database
D. To define test cases only for one specific test file
Solution
Step 1: Understand the role of conftest.py
The conftest.py file 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 B
Quick Check:
conftest.py shares fixtures automatically [OK]
Hint: Remember: conftest.py shares fixtures without imports [OK]
Common Mistakes:
Thinking conftest.py runs tests in parallel
Believing conftest.py stores test data
Assuming fixtures in conftest.py are for one file only
2. Which of the following is the correct way to define a fixture in conftest.py for sharing across test files?
easy
A. def my_fixture(): return 42
B. class my_fixture: pass
C. @pytest.fixture
def my_fixture(): return 42
D. import pytest
my_fixture = 42
Solution
Step 1: Recall fixture syntax in pytest
Fixtures must be decorated with @pytest.fixture to 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 C
Quick Check:
Fixtures need @pytest.fixture decorator [OK]
Hint: Always use @pytest.fixture decorator for fixtures [OK]
Common Mistakes:
Forgetting the @pytest.fixture decorator
Defining fixtures as classes without decorator
Assigning fixture values without function
3. Given the following conftest.py fixture and test file, what will be the output when running pytest?