Test packages help organize many test files into folders. This makes tests easier to find and run together.
Test packages in PyTest
Introduction
Syntax
PyTest
project_root/
tests/
__init__.py
test_module1.py
test_module2.py
subpackage/
__init__.py
test_submodule.pyA test package is a folder with an __init__.py file and test files inside.
pytest automatically finds tests in files named test_*.py or *_test.py.
Examples
tests with two test files.PyTest
# Folder structure example
my_project/
tests/
__init__.py
test_math.py
test_strings.pytests folder and its subfolders.PyTest
# Running tests in a package
$ pytest tests/__init__.py inside the test package.PyTest
# Sharing setup in __init__.py # tests/__init__.py import pytest @pytest.fixture def sample_data(): return [1, 2, 3]
Sample Program
This example shows a test package tests with one test file test_add.py. Running pytest tests/ runs all tests inside.
PyTest
# Folder structure: # my_project/ # tests/ # __init__.py # test_add.py # tests/__init__.py # (empty or can have shared setup) # tests/test_add.py import pytest def add(a, b): return a + b def test_add_positive(): assert add(2, 3) == 5 def test_add_negative(): assert add(-1, -1) == -2 # Run command: # pytest tests/
Important Notes
Always include an __init__.py file in test folders to make them packages.
pytest discovers tests recursively in test packages.
Use descriptive folder and file names to keep tests organized.
Summary
Test packages group test files into folders with __init__.py.
They help organize and run related tests together.
pytest automatically finds and runs tests inside these packages.
Practice
1. What is the main purpose of a test package in
pytest?easy
Solution
Step 1: Understand test package structure
A test package groups related test files inside a folder that contains an__init__.pyfile.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__.pyfile -> Option AQuick Check:
Test packages group tests = A [OK]
Hint: Test packages group tests in folders with __init__.py [OK]
Common Mistakes:
- Confusing test packages with virtual environments
- Thinking test packages run only one file
- Believing pytest installs dependencies automatically
2. Which of the following is the correct way to make a folder a test package in pytest?
easy
Solution
Step 1: Identify the marker for Python packages
Python treats folders with an__init__.pyfile as packages, including test packages.Step 2: Confirm pytest package recognition
pytest uses this file to recognize the folder as a test package and find tests inside it.Final Answer:
Add a file named__init__.pyinside the folder -> Option CQuick Check:
__init__.py marks test packages = B [OK]
Hint: Add __init__.py to folder to make it a test package [OK]
Common Mistakes:
- Adding test files instead of __init__.py to mark package
- Confusing setup.py with package marker
- Using requirements.txt for package identification
3. Given this folder structure:
tests/ __init__.py test_math.py test_string.pyWhat will happen when you run
pytest tests/?medium
Solution
Step 1: Understand pytest test discovery in packages
pytest finds and runs all test files inside a test package folder when you run pytest on that folder.Step 2: Effect of __init__.py on test discovery
The presence of__init__.pymarks the folder as a package but does not prevent pytest from running tests inside.Final Answer:
pytest will run all tests in bothtest_math.pyandtest_string.py-> Option DQuick Check:
pytest runs all tests in package folder = D [OK]
Hint: pytest runs all tests in package folder with __init__.py [OK]
Common Mistakes:
- Thinking __init__.py blocks test discovery
- Assuming only one test file runs
- Expecting errors due to __init__.py presence
4. You have a folder
tests/ with test files but forgot to add __init__.py. What issue might you face?medium
Solution
Step 1: Role of __init__.py in test packages
Without__init__.py, the folder is not a recognized Python package, so pytest may not treat it as a test package.Step 2: Effect on test discovery
pytest might skip tests inside such folders or not group them properly during test runs.Final Answer:
pytest will not recognize the folder as a test package and may skip tests inside -> Option AQuick Check:
Missing __init__.py can cause pytest to skip tests = C [OK]
Hint: Always add __init__.py to test folders to avoid skipped tests [OK]
Common Mistakes:
- Expecting syntax errors without __init__.py
- Thinking pytest auto-creates __init__.py
- Assuming tests run twice without __init__.py
5. 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?hard
Solution
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.Step 2: Confirm effect of nested __init__.py files
Nested__init__.pyfiles mark subfolders as packages but do not affect running tests by folder path.Final Answer:
Runpytest tests/integration/to run only integration tests -> Option BQuick Check:
Run pytest on specific folder to run its tests = A [OK]
Hint: Run pytest on specific test folder to run only those tests [OK]
Common 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
