Why systematic techniques improve coverage in Testing Fundamentals - Automation Benefits in Action
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class CalculatorSystematicTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('http://localhost:8000/calculator') # Replace with actual URL self.wait = WebDriverWait(self.driver, 10) def tearDown(self): self.driver.quit() def calculate_and_verify(self, num1, num2, operation, expected): driver = self.driver wait = self.wait input1 = wait.until(EC.presence_of_element_located((By.ID, 'num1'))) input2 = driver.find_element(By.ID, 'num2') result_field = driver.find_element(By.ID, 'result') input1.clear() input1.send_keys(str(num1)) input2.clear() input2.send_keys(str(num2)) button = driver.find_element(By.ID, operation) button.click() wait.until(lambda d: result_field.text != '') actual_result = result_field.text.strip() self.assertAlmostEqual(float(actual_result), expected, places=4, msg=f'Failed for {operation} with inputs {num1}, {num2}') def test_systematic_coverage(self): test_data = [ (5, 3), (0, 0), (-1, 1) ] for num1, num2 in test_data: self.calculate_and_verify(num1, num2, 'add', num1 + num2) self.calculate_and_verify(num1, num2, 'subtract', num1 - num2) self.calculate_and_verify(num1, num2, 'multiply', num1 * num2) if num2 != 0: self.calculate_and_verify(num1, num2, 'divide', num1 / num2) else: # For division by zero, expect some error or empty result driver = self.driver driver.find_element(By.ID, 'divide').click() result_field = driver.find_element(By.ID, 'result') self.wait.until(lambda d: result_field.text != '') self.assertIn('error', result_field.text.lower() + ' ', 'Expected error message for division by zero') if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework to automate the calculator app.
setUp opens the browser and navigates to the calculator page.
calculate_and_verify is a helper method that inputs numbers, clicks the operation button, waits for the result, and asserts the output matches expected value with 4 decimal precision.
The test_systematic_coverage method runs the helper for three input sets: normal numbers, zeros, and negative/positive mix. This covers typical, boundary, and edge cases systematically.
Explicit waits ensure elements are ready before interaction. Clear locators by ID keep selectors stable. Assertions check correctness precisely.
This approach shows how systematic testing improves coverage by verifying multiple scenarios, not just one.
Now add data-driven testing with 3 different input sets using unittest subTest or pytest parameterize.