0
0
PyTesttesting~20 mins

Conftest.py purpose in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conftest.py Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of conftest.py in pytest

What is the main purpose of the conftest.py file in a pytest testing project?

ATo define fixtures and hooks that can be shared across multiple test files.
BTo list all test cases that pytest should run.
CTo configure the Python interpreter settings for pytest.
DTo store test data files used by tests.
Attempts:
2 left
💡 Hint

Think about how pytest shares setup code between tests.

Predict Output
intermediate
2:00remaining
Fixture scope in conftest.py

Given this conftest.py fixture, what will be the output when running two tests that use it?

import pytest

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

Two tests use this fixture in the same module.

A
Setup resource
Teardown resource
Setup resource
Teardown resource
B
Setup resource
Setup resource
Teardown resource
C
Setup resource
Setup resource
Teardown resource
Teardown resource
D
Setup resource
Teardown resource
Attempts:
2 left
💡 Hint

Remember what scope="module" means for fixture lifetime.

assertion
advanced
1:30remaining
Correct fixture usage from conftest.py

Which assertion correctly tests that a fixture named db_connection from conftest.py returns a non-empty string?

Aassert db_connection != None
Bassert db_connection is None
Cassert isinstance(db_connection, str) and len(db_connection) > 0
Dassert db_connection == ''
Attempts:
2 left
💡 Hint

Check type and length to confirm non-empty string.

🔧 Debug
advanced
2:00remaining
Why fixture from conftest.py is not found?

You have a fixture api_client defined in conftest.py in the root tests folder. When running tests in a subfolder, pytest reports fixture 'api_client' not found. What is the most likely cause?

AThe <code>conftest.py</code> file is missing an import statement for <code>api_client</code>.
BThe <code>conftest.py</code> file is not in a directory recognized by pytest as a test root.
CThe fixture name is misspelled in the test file.
DThe subfolder has its own <code>conftest.py</code> that overrides the root one.
Attempts:
2 left
💡 Hint

Think about how pytest discovers conftest.py files.

framework
expert
2:30remaining
How to share a fixture only for tests in a specific subfolder?

You want a fixture to be available only to tests inside the tests/api folder and its subfolders, but not elsewhere. How should you organize your conftest.py files?

APlace the fixture in <code>tests/api/conftest.py</code> so it applies only to that folder and below.
BPlace the fixture in <code>tests/conftest.py</code> and use <code>scope='folder'</code>.
CDefine the fixture in every test file inside <code>tests/api</code>.
DPlace the fixture in the root <code>conftest.py</code> and add a condition to skip it outside <code>tests/api</code>.
Attempts:
2 left
💡 Hint

Think about how pytest applies conftest.py files based on folder location.