Test functions and classes help organize your checks so you can find and fix problems easily.
Test functions and classes in Selenium Python
Introduction
Syntax
Selenium Python
import unittest class TestExample(unittest.TestCase): def setUp(self): # Runs before each test pass def test_something(self): # Your test code here self.assertEqual(1, 1) def tearDown(self): # Runs after each test pass if __name__ == '__main__': unittest.main()
Use setUp to prepare things before each test.
Use tearDown to clean up after each test.
Examples
Selenium Python
def test_addition(): assert 2 + 2 == 4
Selenium Python
import unittest class TestMath(unittest.TestCase): def test_subtraction(self): self.assertEqual(5 - 3, 2)
Selenium Python
import unittest class TestBrowser(unittest.TestCase): def setUp(self): from selenium import webdriver self.driver = webdriver.Chrome() def test_title(self): self.driver.get('https://example.com') self.assertIn('Example', self.driver.title) def tearDown(self): self.driver.quit()
Sample Program
This test opens the Chrome browser, goes to example.com, checks the page title contains 'Example Domain', then closes the browser.
Selenium Python
import unittest from selenium import webdriver class TestExampleDotCom(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_title_contains_example(self): self.driver.get('https://example.com') self.assertIn('Example Domain', self.driver.title) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
Important Notes
Always close the browser in tearDown to avoid leftover windows.
Use descriptive test method names starting with test_ so the test runner finds them.
Test classes help group related tests and share setup code.
Summary
Test functions and classes organize your checks clearly.
Use setUp and tearDown to prepare and clean up.
Run tests with unittest.main() to see results easily.
Practice
1. What is the main purpose of using test functions and classes in Selenium Python testing?
easy
Solution
Step 1: Understand test organization
Test functions and classes help group related tests logically.Step 2: Recognize ease of running tests
Using classes and functions allows running tests easily with frameworks like unittest.Final Answer:
To organize tests clearly and run them easily -> Option AQuick Check:
Test organization = To organize tests clearly and run them easily [OK]
Hint: Tests group checks for clarity and easy running [OK]
Common Mistakes:
- Thinking tests speed up code execution
- Believing assertions are optional
- Ignoring setup and teardown roles
2. Which of the following is the correct way to define a test method inside a unittest.TestCase class in Selenium Python?
easy
Solution
Step 1: Recall unittest naming conventions
Test methods must start with 'test' and accept 'self' as first parameter.Step 2: Check method definitions
Only 'def test_example(self):' follows correct naming and signature.Final Answer:
def test_example(self): -> Option AQuick Check:
Method name starts with 'test' and has 'self' [OK]
Hint: Test methods start with 'test' and include self [OK]
Common Mistakes:
- Omitting 'self' parameter
- Not starting method name with 'test'
- Using uppercase 'Test' prefix
3. What will be the output when running this test code snippet?
import unittest
class MyTest(unittest.TestCase):
def test_pass(self):
self.assertEqual(2 + 2, 4)
def test_fail(self):
self.assertTrue(False)
if __name__ == '__main__':
unittest.main(exit=False)medium
Solution
Step 1: Analyze test_pass method
assertEqual(2 + 2, 4) is True, so test_pass passes.Step 2: Analyze test_fail method
assertTrue(False) fails, so test_fail fails.Final Answer:
One test passes, one test fails -> Option BQuick Check:
One True, one False assertion = One test passes, one test fails [OK]
Hint: Check each assertion's truth to predict pass/fail [OK]
Common Mistakes:
- Assuming all tests pass
- Ignoring assertTrue(False) fails
- Expecting syntax errors
4. Identify the error in this Selenium Python unittest code:
import unittest
from selenium import webdriver
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_login(self):
self.driver.get('http://example.com')
self.assertIn('Example', self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()medium
Solution
Step 1: Review setup and teardown methods
setUp creates driver; tearDown quits it properly.Step 2: Check assertion and webdriver usage
assertIn checks substring in title correctly; webdriver.Chrome() works if driver is in PATH.Final Answer:
No error; code is correct -> Option CQuick Check:
Standard unittest setup and teardown used [OK]
Hint: Check if setup/teardown and assertions follow unittest rules [OK]
Common Mistakes:
- Assuming webdriver.Chrome() always needs path
- Confusing assertIn with assertEquals
- Forgetting to quit driver in tearDown
5. You want to run multiple Selenium tests that share the same browser setup and cleanup. Which unittest structure best achieves this?
hard
Solution
Step 1: Understand shared setup/cleanup needs
setUpClass and tearDownClass run once for all tests in a class, ideal for shared browser setup.Step 2: Evaluate other options
Separate functions lack shared setup; new webdriver per test is inefficient; unittest.main() runs all tests, not per method.Final Answer:
Use a TestCase class with setUpClass and tearDownClass class methods -> Option DQuick Check:
Shared setup/cleanup = setUpClass/tearDownClass [OK]
Hint: 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
