Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to access the test function name using the request fixture.
PyTest
def test_example(request): test_name = request.[1] assert test_name == 'test_example'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request.param' which is for parameterized fixtures.
Trying to access 'request.config' which is for pytest config.
Using 'request.fixturename' which is not a valid attribute.
✗ Incorrect
The request fixture's node.name attribute gives the current test function's name.
2fill in blank
mediumComplete the code to get the current test module's filename using the request fixture.
PyTest
def test_module_name(request): module_file = request.node.[1] assert module_file.endswith('.py')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'module.file' which does not exist.
Using 'module.filename' which is not a valid attribute.
Using 'module.path' which is not correct.
✗ Incorrect
The request.node.module.__file__ attribute gives the filename of the test module.
3fill in blank
hardFix the error in the code to correctly access the test class name using the request fixture.
PyTest
def test_class_name(request): cls_name = request.node.[1] assert cls_name == 'TestExample'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' keyword which is reserved in Python.
Using 'parent' which is not an attribute of request.node.
Using 'module.__name__' which gives module name, not class name.
✗ Incorrect
The test class name is accessed via 'request.node.cls.__name__'.
4fill in blank
hardFill both blanks to create a fixture that uses the request object to print the current test function name and module.
PyTest
@pytest.fixture def print_test_info(request): print('Running:', request.node.[1]) print('Module:', request.node.[2].__file__)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cls' instead of 'name' for function name.
Using 'parent' which is not valid.
Mixing up module and class attributes.
✗ Incorrect
request.node.name gives the test function name; request.node.module gives the module object.
5fill in blank
hardFill all three blanks to write a fixture that skips a test if the test function name contains 'skip'.
PyTest
import pytest @pytest.fixture(autouse=True) def skip_if_name_contains_skip(request): if 'skip' in request.node.[1]: pytest.[2](reason='Skipped because test name contains skip') yield # Post-test code can go here # Example test def test_skip_example(): assert True
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'skipif' as a function instead of a decorator.
Trying to call 'skip' with parentheses in options.
Using wrong attribute instead of 'name'.
✗ Incorrect
Use 'request.node.name' to get the test name, and 'pytest.skip' to skip the test during runtime.