0
0
PyTesttesting~5 mins

Test modules in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a test module in pytest?
A test module is a Python file that contains test functions or classes. Pytest automatically finds and runs tests inside these modules.
Click to reveal answer
beginner
How should you name a test module for pytest to recognize it automatically?
Name the file starting with test_ or ending with _test.py. For example, test_example.py or example_test.py.
Click to reveal answer
beginner
Why is it important to keep test modules separate from application code?
Separating test modules helps keep code organized, makes tests easier to find, and prevents accidental execution of tests in production.
Click to reveal answer
intermediate
What is the role of __init__.py in test directories?
In pytest, __init__.py files are optional. They can make test directories into packages but are not required for pytest to discover tests.
Click to reveal answer
beginner
How can you run tests from a specific test module using pytest?
Use the command pytest path/to/test_module.py. This runs all tests inside that module only.
Click to reveal answer
Which file name will pytest automatically recognize as a test module?
Amytests.py
Bmath_tested.py
Cexample.py
Dtest_math.py
What does pytest look for inside a test module to run tests?
AFunctions ending with '_check'
BAny function with a docstring
CFunctions or classes starting with 'test'
DOnly classes named 'Test'
If you want to run tests only from a single test module, what command do you use?
Apytest test_module.py
Bpytest -m test_module
Cpytest --module test_module
Dpytest run test_module
Is it necessary to have an __init__.py file in a test directory for pytest to find tests?
ANo, pytest can find tests without it
BYes, always required
COnly if tests are in subdirectories
DOnly for Python versions below 3.6
Why should test modules be kept separate from application code?
ATo reduce file size
BTo organize code and avoid running tests in production
CTo make tests run faster
DTo prevent syntax errors
Explain how pytest discovers and runs tests inside test modules.
Think about file names, function names, and how pytest finds tests.
You got /4 concepts.
    Describe best practices for organizing test modules in a Python project using pytest.
    Consider how to keep tests easy to find and run.
    You got /4 concepts.