A fixture in pytest is a function that sets up some context or data needed for tests. It helps prepare the environment before a test runs and can clean up afterward.
Click to reveal answer
intermediate
What does dynamic fixture selection mean in pytest?
Dynamic fixture selection means choosing which fixture to use during a test run based on some condition, like test parameters or environment, instead of always using the same fixture.
Click to reveal answer
intermediate
How can you select a fixture dynamically in pytest?
You can select a fixture dynamically by using the `request` object inside a fixture or test, or by using `pytest` hooks or parameterization to decide which fixture to use.
Click to reveal answer
advanced
Example: How to use `request.getfixturevalue` for dynamic fixture selection?
Inside a fixture or test, you can call `request.getfixturevalue('fixture_name')` where `'fixture_name'` is a string chosen at runtime. This lets you call different fixtures based on conditions.
Click to reveal answer
beginner
Why is dynamic fixture selection useful?
It helps write flexible tests that can adapt to different setups or inputs without repeating code. This saves time and keeps tests clean and maintainable.
Click to reveal answer
What pytest feature allows you to choose a fixture at runtime?
Aassert statement
Brequest.getfixturevalue()
Cpytest.mark.skip
Dpytest.raises()
✗ Incorrect
The request object has a method getfixturevalue() that lets you get a fixture by name dynamically during test execution.
Which pytest object is commonly used for dynamic fixture selection?
Arequest
Bcapfd
Ctmp_path
Dmonkeypatch
✗ Incorrect
The request object provides access to test context and fixtures, enabling dynamic fixture selection.
Dynamic fixture selection helps to:
ARun the same fixture for all tests
BGenerate test reports
CSkip tests automatically
DChoose fixtures based on conditions during test run
✗ Incorrect
Dynamic fixture selection allows choosing which fixture to use based on runtime conditions.
Which of these is NOT a way to select fixtures dynamically?
AUsing request.getfixturevalue()
BUsing pytest hooks
CUsing assert statements
DUsing parameterization
✗ Incorrect
Assert statements check conditions but do not select fixtures dynamically.
What is the main benefit of dynamic fixture selection?
AMore flexible and reusable tests
BFaster test execution
CAutomatic bug fixing
DBetter test reporting
✗ Incorrect
Dynamic fixture selection makes tests flexible and reusable by adapting fixtures to different needs.
Explain how you can implement dynamic fixture selection in pytest using the request object.
Think about how to get a fixture by name inside another fixture or test.
You got /4 concepts.
Describe why dynamic fixture selection is helpful in writing tests.
Consider how tests can change behavior without rewriting code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of dynamic fixture selection in pytest?
easy
A. To automatically generate test data without fixtures
B. To statically assign fixtures before running tests
C. To choose which fixture to use during test execution based on runtime conditions
D. To skip tests that use fixtures
Solution
Step 1: Understand fixture selection
Dynamic fixture selection means deciding which fixture to use while the test runs, not before.
Step 2: Compare options
Only To choose which fixture to use during test execution based on runtime conditions describes choosing fixtures during test execution based on conditions.
Final Answer:
To choose which fixture to use during test execution based on runtime conditions -> Option C
Quick Check:
Dynamic fixture selection = runtime choice [OK]
Hint: Dynamic means choosing fixtures while tests run [OK]
Common Mistakes:
Confusing static fixture assignment with dynamic selection
Thinking fixtures generate test data automatically
Believing dynamic selection skips tests
2. Which of the following is the correct syntax to select a fixture dynamically inside a test using request.getfixturevalue()?
easy
A. fixture_name = request.getfixturevalue('my_fixture')
B. fixture_name = request.getfixture('my_fixture')
C. fixture_name = request.getfixturevalue(my_fixture)
D. fixture_name = request.getfixture('my_fixture')()
Solution
Step 1: Recall correct method name
The method to get a fixture by name dynamically is getfixturevalue, not getfixture.
Step 2: Check argument type
The fixture name must be a string, so it should be quoted.
Final Answer:
fixture_name = request.getfixturevalue('my_fixture') -> Option A
Quick Check:
Use getfixturevalue('fixture_name') [OK]
Hint: Use getfixturevalue with string fixture name [OK]
Common Mistakes:
Using getfixture instead of getfixturevalue
Passing fixture name without quotes
Calling getfixture as a function
3. Given the code below, what will be the output when running test_dynamic_fixture?
import pytest
@pytest.fixture
def fixture_a():
return 'A'
@pytest.fixture
def fixture_b():
return 'B'
@pytest.mark.parametrize('use_a', [True, False])
def test_dynamic_fixture(request, use_a):
fixture_name = 'fixture_a' if use_a else 'fixture_b'
value = request.getfixturevalue(fixture_name)
print(value)
assert value in ['A', 'B']
medium
A. Prints 'A' twice and passes both tests
B. Prints 'B' then 'A' and fails one test
C. Raises an error because getfixturevalue is used incorrectly
D. Prints 'A' then 'B' and passes both tests
Solution
Step 1: Understand parameterization
The test runs twice: once with use_a=True, once with use_a=False.
Step 2: Trace fixture selection and output
When use_a=True, fixture_name='fixture_a' returns 'A'. When False, fixture_name='fixture_b' returns 'B'. Both print and assert succeed.
Final Answer:
Prints 'A' then 'B' and passes both tests -> Option D
4. Identify the error in the following test code that tries to select a fixture dynamically:
import pytest
@pytest.fixture
def fix1():
return 10
@pytest.fixture
def fix2():
return 20
@pytest.mark.parametrize('name', ['fix1', 'fix2'])
def test_select(request, name):
value = request.getfixture(name)
assert value in [10, 20]
medium
A. Incorrect assert statement
B. Using request.getfixture instead of request.getfixturevalue
C. Missing parentheses when calling fixtures
D. Passing fixture name as a string instead of variable
Solution
Step 1: Check method name for dynamic fixture
The correct method to get a fixture dynamically is getfixturevalue, not getfixture.
Step 2: Confirm usage of fixture name
The fixture name is correctly passed as a string variable, so no issue there.
Final Answer:
Using request.getfixture instead of request.getfixturevalue -> Option B
Quick Check:
Use getfixturevalue, not getfixture [OK]
Hint: Remember method name: getfixturevalue, not getfixture [OK]
Common Mistakes:
Confusing method names for fixture retrieval
Passing fixture name incorrectly
Misunderstanding assert logic
5. You want to write a test that uses different database connection fixtures based on an environment variable DB_TYPE. Which approach correctly implements dynamic fixture selection in pytest?
A) Use request.getfixturevalue() inside the test to select the fixture by name.
B) Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones.
C) Use pytest_generate_tests() hook to parametrize fixtures.
D) Use yield in fixtures to switch dynamically.
hard
A. Use request.getfixturevalue() inside the test to select the fixture by name.
B. Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones.
C. Use pytest_generate_tests() hook to parametrize fixtures.
Choosing a fixture based on an environment variable requires runtime decision inside the test.
Step 2: Evaluate options for dynamic selection
Use request.getfixturevalue() inside the test to select the fixture by name. uses request.getfixturevalue() to pick the fixture by name at runtime, which fits the need.
Step 3: Exclude other options
Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones. applies all fixtures upfront, not dynamic. Use pytest_generate_tests() hook to parametrize fixtures. is for test parametrization, not fixture selection. Use yield in fixtures to switch dynamically. misuses yield; it does not switch fixtures dynamically.
Final Answer:
Use request.getfixturevalue() inside the test to select the fixture by name. -> Option A