Complete the code to run tests in parallel using pytest.
pytest.main(['-n', '[1]'])
Using -n 2 tells pytest to run tests in 2 parallel workers, which enables parallel execution.
Complete the code to import the pytest-xdist plugin for parallel test execution.
import [1]
The correct plugin for parallel execution is pytest-xdist. Importing xdist ensures pytest can use parallel workers.
Fix the error in the pytest command to enable parallel execution with 3 workers.
pytest.main(['-n', '[1]'])
The -n option expects a string number, so '3' is correct. Passing an integer or words causes errors.
Fill both blanks to create a pytest fixture that runs setup and teardown for each test in parallel.
@pytest.fixture(scope=[1]) def setup_teardown(): print('Setup') yield print('Teardown') # Use fixture in test def test_example(setup_teardown): assert 1 == 1
Using scope='function' runs the fixture setup and teardown for each test function, which works well with parallel tests.
Fill all three blanks to define a pytest command line option to control parallel workers with a default of 2.
def pytest_addoption(parser): parser.addoption( '--workers', action='store', default=[1], help='Number of parallel workers' ) def pytest_configure(config): workers = config.getoption('[2]') pytest.main(['-n', [3]])
The default number of workers is set as string '2'. The option name is 'workers'. The variable workers is passed to pytest.main to run tests in parallel.