Bird
Raised Fist0
PyTesttesting~5 mins

Conftest.py purpose in PyTest - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the main purpose of conftest.py in pytest?

conftest.py is used to share fixtures and hooks across multiple test files without importing them explicitly.

Click to reveal answer
beginner
How does pytest find and use conftest.py files?

pytest automatically discovers conftest.py files in test directories and applies their fixtures and hooks to tests in the same or subdirectories.

Click to reveal answer
beginner
Can you import <code>conftest.py</code> directly in your test files?
<p>No, you should not import <code>conftest.py</code> directly. pytest loads it automatically to provide fixtures and hooks.</p>
Click to reveal answer
beginner
What kind of code do you typically put inside conftest.py?

You put reusable fixtures, hooks, and configuration code that multiple test files can use to avoid repetition.

Click to reveal answer
intermediate
Why is using conftest.py better than importing fixtures manually?

It keeps tests cleaner and simpler by avoiding manual imports and allows pytest to manage fixture scope and sharing automatically.

Click to reveal answer
What does conftest.py mainly provide in pytest?
ATest data files
BTest case definitions
CTest reports
DShared fixtures and hooks
Should you import conftest.py in your test files?
ANo, pytest loads it automatically
BYes, always
COnly for fixtures
DOnly for hooks
Where does pytest look for conftest.py files?
AOnly in the root directory
BIn the Python standard library
CIn test directories and subdirectories
DIn the user's home directory
What kind of code is NOT suitable for conftest.py?
ATest case functions
BReusable fixtures
CHooks for pytest events
DConfiguration code
Using conftest.py helps to:
ARun tests in parallel
BAvoid repeating fixture code
CGenerate test reports
DWrite test cases faster
Explain the role of conftest.py in organizing pytest tests.
Think about how pytest finds and uses shared setup code.
You got /4 concepts.
    Describe what kind of code you would put inside conftest.py and why.
    Consider what helps tests run smoothly and share setup.
    You got /4 concepts.

      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