Automation ROI calculation in Testing Fundamentals - Build an Automation Script
import unittest def calculate_roi(manual_exec, manual_maint, auto_exec, auto_maint, executions): total_manual = (manual_exec + manual_maint) * executions total_auto = (auto_exec + auto_maint) * executions if total_auto == 0: raise ValueError("Automated total time cannot be zero") time_saved = total_manual - total_auto roi = (time_saved / total_auto) * 100 return roi class TestAutomationROICalculation(unittest.TestCase): def test_roi_calculation(self): manual_exec = 2.0 # hours manual_maint = 0.5 # hours auto_exec = 0.5 # hours auto_maint = 0.2 # hours executions = 10 roi = calculate_roi(manual_exec, manual_maint, auto_exec, auto_maint, executions) self.assertIsInstance(roi, float, "ROI should be a float") self.assertGreater(roi, 0, "ROI should be greater than 0") def test_zero_automated_time(self): with self.assertRaises(ValueError): calculate_roi(1, 1, 0, 0, 5) if __name__ == '__main__': unittest.main()
The calculate_roi function computes the return on investment percentage by first calculating total manual and automated times, then computing the time saved and ROI percentage. It raises an error if automated total time is zero to avoid division by zero.
The TestAutomationROICalculation class uses Python's unittest framework to test the ROI calculation. It checks that the ROI is a float and greater than zero, matching the expected result. It also tests that the function raises an error when automated time is zero.
This structure separates calculation logic from tests, uses clear variable names, and applies proper assertions for validation.
Now add data-driven testing with 3 different sets of inputs for manual and automated times and executions.