Practice
1. What is the main purpose of
conftest.py in pytest?easy
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]
Hint: Remember: conftest.py shares fixtures across tests [OK]
Common Mistakes:
- Thinking conftest.py runs tests
- Confusing it with test data files
- Assuming it generates reports
2. Which of the following is the correct way to define a fixture in
conftest.py?easy
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]
Hint: Look for @pytest.fixture decorator on a function [OK]
Common Mistakes:
- Defining fixtures without @pytest.fixture
- Using classes instead of functions
- Assigning values without a function
3. Given this
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?medium
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]
Hint: Fixture injects 5, 5*2=10, assertion true [OK]
Common Mistakes:
- Assuming fixture is not found causing error
- Thinking test fails because of wrong value
- Confusing fixture with test parameter
4. You wrote this
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?medium
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]
Hint: Check conftest.py location relative to tests [OK]
Common Mistakes:
- Ignoring file placement rules
- Assuming missing return causes fixture not found
- Not passing fixture as test parameter
5. You want to share a database connection setup across multiple test files using
conftest.py. Which approach best follows pytest best practices?hard
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]
Hint: Use yield fixture for setup and cleanup in conftest.py [OK]
Common Mistakes:
- Duplicating setup in tests
- Not cleaning up resources
- Using global variables instead of fixtures
