Test frameworks often run tests in a specific order or structure. Why is this important?
Think about why test results should not be affected by other tests.
Running tests independently ensures that one test's result does not affect another. This makes test results reliable and easier to debug.
Consider this simplified Selenium Python test code using unittest framework. What will be the printed order of test execution?
import unittest class SampleTest(unittest.TestCase): @classmethod def setUpClass(cls): print('SetupClass') def setUp(self): print('Setup') def test_one(self): print('Test One') def test_two(self): print('Test Two') def tearDown(self): print('TearDown') @classmethod def tearDownClass(cls): print('TearDownClass') if __name__ == '__main__': unittest.main()
Remember the order of setUpClass, setUp, test methods, tearDown, and tearDownClass in unittest.
setUpClass runs once before all tests, setUp runs before each test, tearDown after each test, and tearDownClass once after all tests.
You want to check if the page title is exactly 'Welcome Page'. Which assertion is correct?
from selenium import webdriver browser = webdriver.Chrome() browser.get('http://example.com') page_title = browser.title
Check for exact match of the title string.
Using '==' checks that the page title exactly matches 'Welcome Page'. Other options check partial match, inequality, or invalid comparison.
Look at this Selenium Python code snippet. Why does it raise a NoSuchElementException?
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('http://example.com') element = browser.find_element(By.ID, 'submit-btn') print(element.text)
Check if the element ID matches any element on the loaded page.
NoSuchElementException means Selenium could not find an element with the given locator. Usually the element is missing or the locator is wrong.
Test frameworks isolate each test case execution environment. What is the main reason for this isolation?
Think about test reliability and debugging ease.
Isolation prevents one test's changes from impacting others, making results reliable and debugging simpler.