Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import pytest in a test file.
PyTest
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unittest instead of pytest
Importing unrelated modules
✗ Incorrect
pytest is the testing framework used to write and run tests.
2fill in blank
mediumComplete the code to define a test function inside a test package.
PyTest
def [1](): assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not starting function name with 'test'
Using camelCase instead of snake_case
✗ Incorrect
pytest recognizes functions starting with 'test' as test cases.
3fill in blank
hardFix the error in the test package __init__.py to make it a package.
PyTest
# This file makes the directory a package [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving __init__.py empty
Adding unnecessary code causing errors
✗ Incorrect
An empty __init__.py file with 'pass' makes the folder a package without errors.
4fill in blank
hardFill both blanks to create a test package with a test module and a test function.
PyTest
tests/ __init__.py test_math.py # Inside test_math.py: def [1](): assert 3 * 3 == [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong function name not starting with 'test'
Wrong expected value in assertion
✗ Incorrect
The function name must start with 'test' and the assertion must check the correct product 9.
5fill in blank
hardFill all three blanks to create a test package with a test module, test function, and a failing assertion.
PyTest
tests/ __init__.py test_strings.py # Inside test_strings.py: def [1](): result = 'hello'.upper() assert result == '[2]' assert result != '[3]'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong function name
Wrong expected string in assertion
Confusing uppercase and lowercase
✗ Incorrect
The test function checks that 'hello'.upper() equals 'HELLO' and is not 'hello'.