Bird
Raised Fist0
PyTesttesting~30 mins

Conftest.py purpose in PyTest - Build an Automation Script

Choose your learning style10 modes available

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