Bird
0
0

You want to run a setup step once before all tests in a pytest module and a teardown step once after all tests. Which fixture scope and usage is correct?

hard📝 Application Q8 of 15
Selenium Python - Test Framework Integration (pytest)
You want to run a setup step once before all tests in a pytest module and a teardown step once after all tests. Which fixture scope and usage is correct?
A@pytest.fixture(scope='class') def setup_module(): print('Setup once') yield print('Teardown once')
B@pytest.fixture(scope='module') def setup_module(): print('Setup once') yield print('Teardown once')
C@pytest.fixture(scope='session') def setup_module(): print('Setup once') yield print('Teardown once')
D@pytest.fixture(scope='function') def setup_module(): print('Setup once') yield print('Teardown once')
Step-by-Step Solution
Solution:
  1. Step 1: Understand pytest fixture scopes

    Scope 'module' runs setup once before all tests in the module and teardown after all tests.
  2. Step 2: Match fixture usage to requirement

    Using scope='module' with yield allows setup before and teardown after all tests in the module.
  3. Final Answer:

    @pytest.fixture(scope='module')\ndef setup_module():\n print('Setup once')\n yield\n print('Teardown once') -> Option B
  4. Quick Check:

    Use module scope fixture for once per module setup/teardown [OK]
Quick Trick: Use module scope fixture for setup/teardown once per module [OK]
Common Mistakes:
  • Using function scope runs setup/teardown every test
  • Using session scope runs once per test session, not module
  • Using class scope runs once per class, not module

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes