0
0
PyTesttesting~10 mins

Fixture request object in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Afixturename
Bnode.name
Cparam
Dconfig
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.
2fill in blank
medium

Complete 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'
Amodule.__file__
Bmodule.file
Cmodule.filename
Dmodule.path
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.
3fill in blank
hard

Fix 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'
Aparent.__name__
Bclass.__name__
Ccls.__name__
Dmodule.__name__
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.
4fill in blank
hard

Fill 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'
Aname
Bmodule
Ccls
Dparent
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.
5fill in blank
hard

Fill 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'
Aname
Bskip
Cskipif
Dskip(reason='')
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'.