pytest runs functions starting with test_ automatically as tests.
Step 2: Check each function name
test_add and test_subtract start with test_, so pytest runs them. add does not start with test_, so it is ignored.
Final Answer:
test_add and test_subtract -> Option C
Quick Check:
Functions starting with test_ run as tests [OK]
Hint: Only functions starting with test_ run as tests [OK]
Common Mistakes:
Thinking all functions in test file run as tests
Confusing helper functions with test functions
Ignoring function name prefixes
4. You have a test module named test_math.py with this code:
def test_multiply():
assert 2 * 3 == 5
When you run pytest, what will happen?
medium
A. The test will fail because the assertion is incorrect.
B. pytest will not find any tests to run.
C. pytest will skip the test because of a syntax error.
D. The test will pass because 2 * 3 equals 6.
Solution
Step 1: Analyze the assertion in test_multiply
The assertion checks if 2 * 3 == 5, which is false because 2 * 3 equals 6.
Step 2: Understand pytest test result
pytest runs the test and finds the assertion false, so the test fails.
Final Answer:
The test will fail because the assertion is incorrect. -> Option A
Quick Check:
False assertion causes test failure [OK]
Hint: Check if assertion condition is true or false [OK]
Common Mistakes:
Assuming test passes despite wrong assertion
Confusing syntax errors with assertion failures
Thinking pytest skips failing tests
5. 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?
hard
A. pytest requires you to specify each test module to run tests.
B. pytest runs all test functions from both modules automatically.
C. pytest runs only tests from the first module alphabetically.
D. pytest runs tests only if you import modules manually.
Solution
Step 1: Understand pytest test discovery
pytest automatically finds all test modules named with test_ prefix and runs all test functions inside them.
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_.
Final Answer:
pytest runs all test functions from both modules automatically. -> Option B
Quick Check:
pytest auto-discovers all test modules and functions [OK]
Hint: pytest runs all test_ modules and functions in folder automatically [OK]
Common Mistakes:
Thinking pytest runs only one module at a time
Believing manual imports are needed for tests
Assuming tests must be specified explicitly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.