What is the purpose of a test function in Selenium Python?
A test function is a small piece of code that checks if a specific part of the web application works correctly. It uses Selenium commands to interact with the browser and assertions to verify expected results.
Click to reveal answer
beginner
Why do we use test classes in Selenium Python?
Test classes group related test functions together. They help organize tests, share setup and cleanup code, and make tests easier to maintain and read.
Click to reveal answer
beginner
What is the role of the setup method in a test class?
The setup method runs before each test function in the class. It prepares the environment, like opening a browser, so each test starts fresh and independent.
Click to reveal answer
beginner
How do assertions work in test functions?
Assertions check if the actual result matches the expected result. If the assertion fails, the test stops and reports a failure, helping find bugs early.
Click to reveal answer
beginner
Give an example of a simple test function using Selenium Python.
Example:
from selenium import webdriver
def test_title():
driver = webdriver.Chrome()
driver.get('https://example.com')
assert 'Example Domain' in driver.title
driver.quit()
This test opens a browser, goes to example.com, checks the page title, and closes the browser.
Click to reveal answer
What does a test class help you do in Selenium Python?
AAutomatically fix bugs in the code
BRun tests faster by skipping assertions
CGroup related tests and share setup code
DReplace the need for test functions
✗ Incorrect
Test classes group related tests and allow sharing setup and cleanup code to keep tests organized.
Which method usually runs before each test function in a test class?
Asetup()
Bteardown()
Cmain()
D__init__()
✗ Incorrect
The setup() method prepares the environment before each test runs.
What happens if an assertion fails in a test function?
AThe test continues running
BThe test passes anyway
CThe browser closes automatically
DThe test stops and reports failure
✗ Incorrect
An assertion failure stops the test and reports the failure to help identify issues.
Which Selenium command opens a web page in a browser?
Adriver.open()
Bdriver.get()
Cdriver.load()
Ddriver.start()
✗ Incorrect
driver.get(url) opens the specified web page in the browser.
Why is it important to close the browser after a test?
ATo save computer memory and avoid interference with other tests
BTo speed up the test execution
CTo automatically fix errors
DTo restart the test automatically
✗ Incorrect
Closing the browser frees resources and ensures tests do not affect each other.
Explain how you would organize multiple Selenium tests using test functions and classes.
Think about keeping tests clean and easy to manage.
You got /4 concepts.
Describe the role of assertions in test functions and what happens when they fail.
Assertions are like checkpoints in your test.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using test functions and classes in Selenium Python testing?
easy
A. To organize tests clearly and run them easily
B. To make the code run faster
C. To avoid writing any assertions
D. To skip browser setup
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 A
Quick 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
A. def test_example(self):
B. def example_test():
C. def TestExample(self):
D. def _test_example(self):
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 A
Quick 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
A. Both tests pass
B. One test passes, one test fails
C. Both tests fail
D. Syntax error occurs
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 B
Quick 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
A. webdriver.Chrome() requires path argument
B. Missing call to super() in setUp and tearDown
C. No error; code is correct
D. assertIn should be assertEquals
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 C
Quick 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
A. Call unittest.main() inside each test method
B. Write separate test functions without any setup or teardown
C. Create a new webdriver instance inside each test method
D. Use a TestCase class with setUpClass and tearDownClass class methods
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 D