Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of conftest.py in pytest?
easy
A. To generate test reports in HTML
B. To store test data in JSON format
C. To share setup code and fixtures across multiple test files
D. To run tests in parallel automatically

Solution

  1. Step 1: Understand the role of conftest.py

    conftest.py is used to hold fixtures and setup code that multiple test files can use.
  2. Step 2: Compare with other options

    Options A, B, and C describe other pytest features or tools but not the purpose of conftest.py.
  3. Final Answer:

    To share setup code and fixtures across multiple test files -> Option C
  4. Quick 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
A. def setup(): pass
B. @pytest.fixture def my_fixture(): return 42
C. class Fixture: pass
D. import pytest my_fixture = 42

Solution

  1. Step 1: Recall fixture syntax in pytest

    Fixtures are defined using the @pytest.fixture decorator followed by a function.
  2. Step 2: Check each option

    @pytest.fixture def my_fixture(): return 42 correctly uses @pytest.fixture and defines a function returning a value. Others either lack the decorator or are incorrect syntax.
  3. Final Answer:

    @pytest.fixture\ndef my_fixture(): return 42 -> Option B
  4. Quick 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
A. Test passes successfully
B. Test fails with assertion error
C. Test raises a NameError
D. Test is skipped automatically

Solution

  1. Step 1: Understand fixture usage

    The fixture number returns 5 and is automatically injected into the test function.
  2. Step 2: Evaluate the assertion

    The test asserts number * 2 == 10, which is 5 * 2 == 10, true, so the test passes.
  3. Final Answer:

    Test passes successfully -> Option A
  4. Quick 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
A. The test function does not accept data as a parameter
B. The fixture function is missing the return statement
C. The fixture is defined with a wrong decorator like @pytest.test
D. The conftest.py file is not in the same or parent directory of the test file

Solution

  1. Step 1: Understand pytest fixture discovery

    Pytest finds conftest.py files in the test directory or its parents. If conftest.py is elsewhere, fixtures won't be found.
  2. Step 2: Analyze other options

    Options A, B, and D would cause different errors or test failures, but not 'fixture not found'.
  3. Final Answer:

    The conftest.py file is not in the same or parent directory of the test file -> Option D
  4. Quick 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
A. Define a fixture in conftest.py that creates and yields the connection, then closes it after tests
B. Write the connection setup code inside each test function separately
C. Create a global variable in conftest.py holding the connection without a fixture
D. Use a class with static methods in conftest.py to manage the connection

Solution

  1. Step 1: Understand fixture scope and cleanup

    Using a fixture with yield allows setup before tests and cleanup after, ideal for resources like DB connections.
  2. Step 2: Evaluate other options

    Write the connection setup code inside each test function separately repeats code, violating DRY. Create a global variable in conftest.py holding the connection without a fixture lacks cleanup and is not recommended. Use a class with static methods in conftest.py to manage the connection is more complex and less idiomatic.
  3. Final Answer:

    Define a fixture in conftest.py that creates and yields the connection, then closes it after tests -> Option A
  4. Quick 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard