0
0
PyTesttesting~20 mins

Conftest fixtures (shared across files) in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conftest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of shared fixture usage across test files
Given the following conftest.py fixture and two test files, what will be the output when running all tests?
PyTest
## conftest.py
import pytest

@pytest.fixture(scope='module')
def resource():
    print('Setup resource')
    yield 'resource_data'
    print('Teardown resource')

## test_file1.py

def test_one(resource):
    assert resource == 'resource_data'

## test_file2.py

def test_two(resource):
    assert resource == 'resource_data'
ASetup resource\nSetup resource\nTeardown resource\nTeardown resource
BSetup resource\nTeardown resource
CSetup resource\nTeardown resource\nTeardown resource
DSetup resource\nTeardown resource\nSetup resource\nTeardown resource
Attempts:
2 left
💡 Hint
Think about the scope='module' fixture and how many times it runs across multiple test files.
assertion
intermediate
1:30remaining
Correct assertion for fixture data modification
A conftest fixture returns a list. A test modifies the list by appending an item. Which assertion correctly verifies the list content inside the test?
PyTest
import pytest

@pytest.fixture
def sample_list():
    return [1, 2, 3]

def test_append(sample_list):
    sample_list.append(4)
    # Which assertion is correct here?
Aassert sample_list == [4, 1, 2, 3]
Bassert sample_list == [1, 2, 3, 4]
Cassert sample_list != [1, 2, 3, 4]
Dassert sample_list is [1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Remember how list append works and how to compare lists in Python.
🔧 Debug
advanced
2:00remaining
Identify the cause of fixture not being shared
You have a fixture in conftest.py with scope='session', but it seems to run before every test function instead of once per session. What is the most likely cause?
PyTest
## conftest.py
import pytest

@pytest.fixture(scope='session')
def db_connection():
    print('Connecting to DB')
    yield 'db_conn'
    print('Closing DB')

## test_module.py

def test_a(db_connection):
    assert db_connection == 'db_conn'

def test_b(db_connection):
    assert db_connection == 'db_conn'
AThe fixture is defined in conftest.py but tests are run with '-k' option filtering only one test at a time.
BThe fixture is not imported in test_module.py, so it runs multiple times.
CThe fixture scope='session' is ignored if tests are in different files.
DThe fixture uses yield, which forces it to run before every test.
Attempts:
2 left
💡 Hint
Think about how pytest runs tests and how filtering affects fixture scope.
framework
advanced
1:30remaining
Best practice for fixture sharing in large projects
In a large pytest project with many test files, where should you place fixtures that need to be shared across multiple test files?
APut all fixtures in a separate Python module and import them manually in each test file.
BDefine fixtures inside each test file that uses them to avoid conflicts.
CPlace shared fixtures in a conftest.py file at the root or common directory.
DDefine fixtures inside test classes to scope them to tests only.
Attempts:
2 left
💡 Hint
Think about pytest's automatic fixture discovery.
🧠 Conceptual
expert
2:00remaining
Understanding fixture scope and autouse behavior
Consider a fixture in conftest.py defined with scope='function' and autouse=True. How many times will this fixture run if you have 3 test functions in 2 different test files?
A3 times total, once per test function across all files.
B6 times total, once per test function in each file separately.
COnce per test file, so 2 times total.
DOnce per test session, so only 1 time.
Attempts:
2 left
💡 Hint
Remember what scope='function' and autouse=True mean for fixture execution.