0
0
PyTesttesting~20 mins

Test packages in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding pytest test package structure

Which of the following directory structures correctly represents a pytest test package that will be automatically discovered and run by pytest?

Aproject_root/tests/test_module.txt
Bproject_root/tests/__init__.py and project_root/tests/test_module.py
Cproject_root/test_module.py only
Dproject_root/tests/test_module.py
Attempts:
2 left
💡 Hint

pytest discovers tests in files named test_*.py or *_test.py in any directory, even without __init__.py.

Predict Output
intermediate
2:00remaining
pytest test package import behavior

Given the following package structure and test code, what will be the output when running pytest from project_root?

project_root/
  mypackage/
    __init__.py
    utils.py
  tests/
    __init__.py
    test_utils.py

# test_utils.py content:
import mypackage.utils

def test_example():
    assert mypackage.utils.func() == 42

# utils.py content:
def func():
    return 42
ATest passes successfully
BImportError: No module named 'mypackage.utils'
CAssertionError because func() returns wrong value
DSyntaxError in test_utils.py
Attempts:
2 left
💡 Hint

pytest adds the project root to sys.path, so imports from sibling packages work.

assertion
advanced
2:00remaining
Correct assertion for test package content

You want to write a pytest test to check that the tests package contains exactly 3 test files named test_*.py. Which assertion correctly verifies this?

PyTest
import os

def test_number_of_test_files():
    test_dir = os.path.join(os.path.dirname(__file__), '..', 'tests')
    test_files = [f for f in os.listdir(test_dir) if f.startswith('test_') and f.endswith('.py')]
    # Which assertion is correct here?
Aassert test_files == 3
Bassert test_files.length == 3
Cassert len(test_files) == 3
Dassert len(test_files) > 3
Attempts:
2 left
💡 Hint

Use len() to get the number of items in a list.

🔧 Debug
advanced
2:00remaining
Debugging pytest test package import error

You have this structure:

project_root/
  app/
    __init__.py
    module.py
  tests/
    test_module.py

In test_module.py, you write import app.module but get ModuleNotFoundError when running pytest. What is the most likely cause?

AYou ran pytest from inside the tests directory instead of project_root
BYou forgot to add __init__.py in the tests directory
Cmodule.py has syntax errors preventing import
Dpytest does not support imports from sibling packages
Attempts:
2 left
💡 Hint

pytest uses the current working directory to set the Python path.

framework
expert
2:00remaining
Configuring pytest to recognize a test package outside default locations

You have a test package located at project_root/integration_tests/ which contains test files. By default, pytest does not discover tests there. Which configuration change will make pytest discover tests in integration_tests?

ARun pytest with <code>pytest -k integration_tests</code>
BAdd a pytest.ini file with <code>[pytest]\ntestpaths = integration_tests</code>
CRename integration_tests to tests and add __init__.py
DAdd __init__.py to integration_tests and run pytest normally
Attempts:
2 left
💡 Hint

pytest.ini can specify directories to search for tests.