Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pytest_register' instead of 'pytest_plugins'.
Confusing function names with plugin registration.
✗ Incorrect
The variable 'pytest_plugins' is used to register plugins in PyTest.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'store_true' which expects no value.
Using 'append' which collects multiple values.
✗ Incorrect
The 'store' action saves the option value as a string.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Including dashes in the option name causes errors.
Using a different option name than defined.
✗ Incorrect
The getoption method expects the option name without dashes.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping option and marker names.
Using incorrect strings for option or marker.
✗ Incorrect
The option name is 'skip_slow' and the marker to check is 'slow'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between marker name and usage.
Wrong description text.
✗ Incorrect
The marker name is 'slow', description is 'slow tests', and test uses '@pytest.mark.slow'.