0
0
Testing Fundamentalstesting~15 mins

Defect density and detection rate in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Calculate Defect Density and Defect Detection Rate
Preconditions (3)
Step 1: Count the total number of defects found during testing
Step 2: Note the size of the software module in lines of code
Step 3: Calculate defect density as (number of defects found) / (size of module in KLOC)
Step 4: Calculate defect detection rate as (number of defects found) / (total defects injected)
Step 5: Verify the calculated defect density and detection rate values
✅ Expected Result: Defect density and defect detection rate are correctly calculated and displayed as decimal numbers
Automation Requirements - Python unittest
Assertions Needed:
Assert defect density is correctly calculated as defects_found / (module_size / 1000)
Assert defect detection rate is correctly calculated as defects_found / total_defects_injected
Best Practices:
Use clear function names for calculations
Use assertAlmostEqual for floating point comparisons
Keep test data simple and clear
Automated Solution
Testing Fundamentals
import unittest

class DefectMetrics:
    @staticmethod
    def calculate_defect_density(defects_found: int, module_size_loc: int) -> float:
        # Convert lines of code to KLOC
        kloc = module_size_loc / 1000
        if kloc == 0:
            return 0.0
        return defects_found / kloc

    @staticmethod
    def calculate_detection_rate(defects_found: int, total_defects_injected: int) -> float:
        if total_defects_injected == 0:
            return 0.0
        return defects_found / total_defects_injected

class TestDefectMetrics(unittest.TestCase):
    def test_defect_density(self):
        defects_found = 50
        module_size_loc = 10000  # 10 KLOC
        expected_density = 5.0  # 50 / 10
        density = DefectMetrics.calculate_defect_density(defects_found, module_size_loc)
        self.assertAlmostEqual(density, expected_density, places=5)

    def test_detection_rate(self):
        defects_found = 40
        total_defects_injected = 50
        expected_rate = 0.8  # 40 / 50
        rate = DefectMetrics.calculate_detection_rate(defects_found, total_defects_injected)
        self.assertAlmostEqual(rate, expected_rate, places=5)

    def test_zero_module_size(self):
        density = DefectMetrics.calculate_defect_density(10, 0)
        self.assertEqual(density, 0.0)

    def test_zero_total_defects(self):
        rate = DefectMetrics.calculate_detection_rate(10, 0)
        self.assertEqual(rate, 0.0)

if __name__ == '__main__':
    unittest.main()

The DefectMetrics class has two static methods to calculate defect density and detection rate. We convert lines of code to KLOC for defect density calculation. We handle zero division by returning 0.0 if module size or total defects are zero.

The TestDefectMetrics class uses Python's unittest framework. It tests normal cases and edge cases (zero module size and zero total defects). We use assertAlmostEqual for floating point checks to avoid precision issues.

This structure keeps calculations separate from tests, making the code clear and maintainable.

Common Mistakes - 3 Pitfalls
Dividing defects found by lines of code instead of KLOC
Not handling division by zero when module size or total defects are zero
Using assertEqual for floating point numbers
Bonus Challenge

Now add data-driven testing with 3 different sets of defects found, module sizes, and total defects injected.

Show Hint