Which of the following best explains why advanced fixtures are used in pytest for complex test scenarios?
Think about how fixtures help organize repeated preparation steps.
Advanced fixtures help manage setup and cleanup code efficiently, especially when tests share common resources or need complex preparation. This reduces duplication and improves maintainability.
What will be the output when running the following pytest code?
import pytest @pytest.fixture(scope='module') def resource(): print('Setup resource') yield 'data' print('Teardown resource') def test_one(resource): print(f'Test one uses {resource}') def test_two(resource): print(f'Test two uses {resource}')
Consider how the 'module' scope affects fixture setup and teardown timing.
With scope='module', the fixture runs setup once before any tests in the module and teardown after all tests finish. So 'Setup resource' prints once, then both tests run, then 'Teardown resource' prints last.
Given a pytest fixture that yields a dictionary {'key': 'value'}, which assertion correctly verifies the fixture's output in a test?
import pytest @pytest.fixture def sample_data(): yield {'key': 'value'} def test_data(sample_data): # Which assertion is correct here?
Remember how to compare dictionaries and use correct assertion syntax.
Option A correctly compares the entire dictionary. Option A uses assignment instead of comparison. Option A tries to access a dict key as an attribute, which raises an error. Option A uses a method that does not exist.
What error will occur when running this pytest code?
import pytest @pytest.fixture def number(): return 5 def test_number(number): assert number == 5 def test_double(number): number = number * 2 assert number == 10 def test_fail(number): number.append(1)
Check the type of the fixture return value and what methods are called on it.
The fixture returns an integer 5. The test_fail tries to call append() on an int, which causes AttributeError.
Which statement best describes how advanced pytest fixtures improve test reliability in complex scenarios?
Think about how fixtures control setup and cleanup to avoid tests affecting each other.
Advanced fixtures help keep tests independent by managing setup and teardown carefully, avoiding shared state issues and side effects that can cause flaky tests.