0
0
PyTesttesting~10 mins

Why plugins extend PyTest capabilities - Test Your Understanding

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

Complete the code to import the PyTest plugin system.

PyTest
import pytest

@pytest.hookimpl(tryfirst=True)
def pytest_addoption(parser):
    parser.addoption('--myopt', action='store', default='default', help='My option')

# Plugin registration uses [1]
Drag options to blanks, or click blank then click option'
Apytest_register
Bpytest_addoption
Cpytest_plugins
Dpytest_plugin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pytest_register' instead of 'pytest_plugins'.
Confusing function names with plugin registration.
2fill in blank
medium

Complete the code to add a command line option using a PyTest plugin.

PyTest
def pytest_addoption(parser):
    parser.addoption('--env', action=[1], default='dev', help='Set environment')
Drag options to blanks, or click blank then click option'
A'store'
B'store_true'
C'append'
D'count'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'store_true' which expects no value.
Using 'append' which collects multiple values.
3fill in blank
hard

Fix the error in the fixture that uses a plugin option.

PyTest
@pytest.fixture
def env(request):
    return request.config.getoption([1])
Drag options to blanks, or click blank then click option'
A'--environment'
B'--env'
C'environment'
D'env'
Attempts:
3 left
💡 Hint
Common Mistakes
Including dashes in the option name causes errors.
Using a different option name than defined.
4fill in blank
hard

Fill both blanks to create a plugin hook that modifies test collection.

PyTest
def pytest_collection_modifyitems(config, items):
    if config.getoption([1]):
        items[:] = [item for item in items if item.get_closest_marker([2]) is None]
Drag options to blanks, or click blank then click option'
A'skip_slow'
B'slow'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping option and marker names.
Using incorrect strings for option or marker.
5fill in blank
hard

Fill all three blanks to define a plugin that adds a custom marker and uses it in a test.

PyTest
def pytest_configure(config):
    config.addinivalue_line('markers', '[1]: mark test as [2]')

@pytest.mark.[3]
def test_example():
    assert True
Drag options to blanks, or click blank then click option'
Aslow
Bslow tests
Dfast
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between marker name and usage.
Wrong description text.