Bird
0
0

You want to run multiple Selenium tests that share the same browser setup and cleanup. Which unittest structure best achieves this?

hard📝 Application Q15 of 15
Selenium Python - Test Framework Integration (pytest)
You want to run multiple Selenium tests that share the same browser setup and cleanup. Which unittest structure best achieves this?
ACall unittest.main() inside each test method
BWrite separate test functions without any setup or teardown
CCreate a new webdriver instance inside each test method
DUse a TestCase class with setUpClass and tearDownClass class methods
Step-by-Step Solution
Solution:
  1. Step 1: Understand shared setup/cleanup needs

    setUpClass and tearDownClass run once for all tests in a class, ideal for shared browser setup.
  2. Step 2: Evaluate other options

    Separate functions lack shared setup; new webdriver per test is inefficient; unittest.main() runs all tests, not per method.
  3. Final Answer:

    Use a TestCase class with setUpClass and tearDownClass class methods -> Option D
  4. Quick Check:

    Shared setup/cleanup = setUpClass/tearDownClass [OK]
Quick Trick: Use setUpClass/tearDownClass for shared browser setup [OK]
Common Mistakes:
  • Creating driver in each test wastes resources
  • Calling unittest.main() multiple times causes errors
  • Skipping setup causes flaky tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes