A test package is a directory containing multiple test modules and an __init__.py file, allowing pytest to discover and run tests organized in that directory.
Click to reveal answer
beginner
Why do we need an __init__.py file in a pytest test package?
The __init__.py file makes the directory a Python package, which helps pytest recognize it as a test package and allows importing modules within it.
Click to reveal answer
beginner
How does pytest discover tests inside a test package?
Pytest looks for files matching test_*.py or *_test.py inside the package directory and runs test functions or methods inside those files.
Click to reveal answer
intermediate
Can you organize tests in sub-packages inside a pytest test package?
Yes, you can create subdirectories with __init__.py files inside a test package to organize tests hierarchically, and pytest will discover tests recursively.
Click to reveal answer
intermediate
What is a practical benefit of using test packages in pytest?
Test packages help keep tests organized by feature or module, making it easier to maintain and run specific groups of tests without running everything.
Click to reveal answer
What file must be present in a directory for pytest to treat it as a test package?
Asetup.py
B__init__.py
Cconftest.py
Drequirements.txt
✗ Incorrect
The __init__.py file marks the directory as a Python package, which pytest uses to recognize test packages.
Which file name pattern does pytest use to find test modules inside a test package?
Atest_*.py or *_test.py
Bmodule_*.py
Ctestpackage.py
Dsetup_*.py
✗ Incorrect
Pytest looks for files starting with test_ or ending with _test to find test modules.
Can pytest discover tests in subdirectories inside a test package?
AOnly if tests are in a single file
BNo, pytest only looks in the top-level directory
CYes, if subdirectories have __init__.py files
DOnly if tests are imported manually
✗ Incorrect
Subdirectories with __init__.py files are treated as sub-packages, allowing pytest to discover tests recursively.
What is the main advantage of organizing tests into packages?
ABetter organization and selective test running
BFaster test execution
CAutomatic bug fixing
DNo need for test functions
✗ Incorrect
Organizing tests into packages helps keep tests structured and allows running specific groups of tests easily.
If a directory lacks __init__.py, what happens when pytest tries to discover tests there?
AIt will fail to find any tests
BIt will run tests twice
CIt will automatically create __init__.py
DIt treats it as a normal directory, not a package, which may affect imports
✗ Incorrect
Without __init__.py, the directory is not a package, so relative imports inside tests may fail, but pytest can still find tests by filename.
Explain how to create and organize a test package in pytest.
Think about Python packages and test discovery rules.
You got /4 concepts.
Describe the benefits of using test packages when writing tests with pytest.
Consider how grouping tests helps in real projects.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a test package in pytest?
easy
A. To group related test files into a folder with an __init__.py file
B. To run tests only from a single test file
C. To create a virtual environment for tests
D. To install pytest dependencies automatically
Solution
Step 1: Understand test package structure
A test package groups related test files inside a folder that contains an __init__.py file.
Step 2: Purpose of grouping tests
This grouping helps organize tests and allows pytest to find and run them together.
Final Answer:
To group related test files into a folder with an __init__.py file -> Option A
Quick Check:
Test packages group tests = A [OK]
Hint: Test packages group tests in folders with __init__.py [OK]