Bird
0
0

How can you combine pytest fixtures with test classes to share browser setup and teardown for Selenium tests?

hard📝 Application Q9 of 15
Selenium Python - Test Framework Integration (pytest)
How can you combine pytest fixtures with test classes to share browser setup and teardown for Selenium tests?
AUse setUp and tearDown methods inside the test class only.
BUse global variables to store browser instance across tests.
CCreate a base class with __init__ method to open and close the browser.
DDefine a fixture with scope='class' and use it as a parameter in test methods.
Step-by-Step Solution
Solution:
  1. Step 1: Understand pytest fixture scopes

    Fixtures with scope='class' run once per test class, ideal for browser setup/teardown.
  2. Step 2: Use fixtures as parameters in test methods

    Passing fixture as parameter injects the browser instance into tests cleanly.
  3. Final Answer:

    Define a fixture with scope='class' and use it as a parameter in test methods. -> Option D
  4. Quick Check:

    pytest class-level setup = fixture with scope='class' [OK]
Quick Trick: Use class-scoped fixtures for shared Selenium setup [OK]
Common Mistakes:
  • Relying only on setUp/tearDown in pytest
  • Using __init__ for browser setup
  • Using global variables for browser instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes