0
0
PyTesttesting~5 mins

Test packages in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a test package in pytest?
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
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
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
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
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
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.