Consider the following pytest code with autouse fixtures. What will be the output when running the test?
import pytest @pytest.fixture(autouse=True) def setup_one(): print("Setup One") @pytest.fixture(autouse=True) def setup_two(): print("Setup Two") def test_example(): print("Test Running")
Autouse fixtures run before the test function in the order they are defined.
Pytest runs autouse fixtures in the order they appear in the code before the test function. So 'setup_one' prints first, then 'setup_two', then the test prints.
You have an autouse fixture that appends 'setup' to a list before each test. Which assertion correctly verifies the list contains 'setup' after the test runs?
import pytest results = [] @pytest.fixture(autouse=True) def add_setup(): results.append('setup') def test_check(): # Which assertion below is correct? pass
The fixture adds 'setup' to the list before each test, so the list should contain 'setup'.
Option A checks if 'setup' is in the list, which is true after the fixture runs. Option A may fail if multiple tests run and append multiple 'setup' entries. Options C and D are incorrect.
Given this pytest code, why does the autouse fixture not run before the test?
import pytest @pytest.fixture(autouse=True) def setup(): print("Setup running") def test_sample(): print("Test running")
Autouse fixtures must be visible to the test, usually in the same module or conftest.py.
If the fixture is defined in another module but not imported or in conftest.py, pytest won't run it automatically. The other options are incorrect because autouse fixtures do not require @usefixtures, test name is correct, and autouse works for function tests too.
Which statement correctly describes how autouse fixtures behave with different scopes in pytest?
Fixture scope controls how often the fixture runs, autouse controls if it runs automatically.
Autouse fixtures respect their scope. A 'module' scoped autouse fixture runs once before any tests in that module. 'Function' scope runs before each test function. Option D is wrong because 'function' scope runs per test, not per session. Option D is false because scope matters. Option D is false; autouse fixtures can have 'class' scope.
Which of the following is the best explanation of the impact of using autouse fixtures extensively in a large pytest suite?
Think about how often autouse fixtures run and what they do.
Autouse fixtures run automatically before tests, which helps ensure setup runs consistently, improving isolation. However, if the fixture does expensive work and has a narrow scope like 'function', it can slow down the suite. They do not inherently speed up tests or prevent parallelism.