Bird
0
0

How does pytest handle these test modules when you run pytest in the folder?

hard🚀 Application Q15 of 15
PyTest - Test Organization
You want to organize tests for a calculator app. You create two files: test_add.py and test_subtract.py. Each file has multiple test functions starting with test_. How does pytest handle these test modules when you run pytest in the folder?
Apytest requires you to specify each test module to run tests.
Bpytest runs all test functions from both modules automatically.
Cpytest runs only tests from the first module alphabetically.
Dpytest runs tests only if you import modules manually.
Step-by-Step Solution
Solution:
  1. Step 1: Understand pytest test discovery

    pytest automatically finds all test modules named with test_ prefix and runs all test functions inside them.
  2. Step 2: Apply to multiple test modules

    When running pytest in the folder, it collects tests from both test_add.py and test_subtract.py and runs all test functions starting with test_.
  3. Final Answer:

    pytest runs all test functions from both modules automatically. -> Option B
  4. Quick Check:

    pytest auto-discovers all test modules and functions [OK]
Quick Trick: pytest runs all test_ modules and functions in folder automatically [OK]
Common Mistakes:
MISTAKES
  • Thinking pytest runs only one module at a time
  • Believing manual imports are needed for tests
  • Assuming tests must be specified explicitly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes