Bird
0
0

You want to organize tests for a large project using pytest. You create nested folders with __init__.py files like this:

hard🚀 Application Q15 of 15
PyTest - Test Organization
You want to organize tests for a large project using pytest. You create nested folders with __init__.py files like this:
tests/
  __init__.py
  unit/
    __init__.py
    test_utils.py
  integration/
    __init__.py
    test_api.py
How can you run only the integration tests using pytest?
ARun <code>pytest tests/</code> and pytest will run only integration tests automatically
BRun <code>pytest tests/integration/</code> to run only integration tests
CRun <code>pytest tests/unit/</code> to run integration tests
DRemove __init__.py files to run only integration tests
Step-by-Step Solution
Solution:
  1. Step 1: Understand folder-based test selection

    pytest runs tests in the folder path you specify. To run only integration tests, specify the integration folder.
  2. Step 2: Confirm effect of nested __init__.py files

    Nested __init__.py files mark subfolders as packages but do not affect running tests by folder path.
  3. Final Answer:

    Run pytest tests/integration/ to run only integration tests -> Option B
  4. Quick Check:

    Run pytest on specific folder to run its tests = A [OK]
Quick Trick: Run pytest on specific test folder to run only those tests [OK]
Common Mistakes:
MISTAKES
  • Expecting pytest to auto-filter integration tests without folder path
  • Running wrong folder path for desired tests
  • Removing __init__.py thinking it filters tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes