Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a fixture with session scope.
PyTest
import pytest @pytest.fixture(scope=[1]) def setup_db(): print("Setup DB") yield print("Teardown DB")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'function' scope when you want to share setup across tests.
Confusing 'module' and 'session' scopes.
✗ Incorrect
The 'session' scope means the fixture is created once per test session, ideal for expensive setup shared across tests.
2fill in blank
mediumComplete the code to run tests in parallel using pytest-xdist.
PyTest
# Run tests with pytest-xdist pytest -n [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid values like 'all' or 'parallel' for '-n'.
Not installing pytest-xdist before using '-n'.
✗ Incorrect
Using '-n auto' lets pytest-xdist automatically decide the number of CPUs to use for parallel tests.
3fill in blank
hardFix the error in the fixture to avoid sharing state in parallel tests.
PyTest
import pytest @pytest.fixture(scope="session") def resource(): data = [] yield data data.clear() # Tests modify resource by appending values # Problem: parallel tests share the same list causing conflicts # Fix by changing [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Keeping session scope and causing test interference.
Using global variables which are shared across tests.
✗ Incorrect
Changing the fixture scope to 'function' ensures each test gets its own fresh list, avoiding shared state issues in parallel runs.
4fill in blank
hardFill both blanks to create a fixture that runs once per module and uses autouse.
PyTest
@pytest.fixture(scope=[1], autouse=[2]) def setup_env(): print("Setup environment") yield print("Teardown environment")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autouse to False and expecting automatic use.
Using function scope when module scope is needed.
✗ Incorrect
Setting scope to 'module' runs the fixture once per module. Autouse=True makes it run automatically for all tests in the module.
5fill in blank
hardFill all three blanks to create a parametrized fixture with session scope and proper teardown.
PyTest
@pytest.fixture(scope=[1], params=["chrome", "firefox"]) def browser(request): driver = start_driver([2]) yield driver [3](driver)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using function scope instead of session for sharing.
Not using request.param to get parameter value.
Forgetting to stop the driver in teardown.
✗ Incorrect
Session scope shares the fixture for the session. request.param accesses the current parameter. stop_driver properly closes the driver after test.