Defect density and detection rate in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of defects found, module sizes, and total defects injected.
Practice
defect density measure in software testing?Solution
Step 1: Understand the definition of defect density
Defect density is defined as the number of defects found divided by the size of the software (e.g., lines of code or function points).Step 2: Compare options with the definition
Only Number of defects per size of software correctly describes defect density as defects per size of software.Final Answer:
Number of defects per size of software -> Option CQuick Check:
Defect density = Defects / Size [OK]
- Confusing defect density with total defects
- Mixing defect density with test case count
- Thinking defect density measures fix time
detection rate during testing?Solution
Step 1: Recall detection rate meaning
Detection rate measures how many defects testing finds out of all defects present.Step 2: Match formula to definition
(Number of defects found) / (Total defects present) correctly shows detection rate as defects found divided by total defects present.Final Answer:
(Number of defects found) / (Total defects present) -> Option AQuick Check:
Detection rate = Found / Total [OK]
- Reversing numerator and denominator
- Confusing detection rate with pass rate
- Using fixed defects instead of found defects
Solution
Step 1: Apply defect density formula
Defect density = Number of defects / Size of software = 25 / 5000.Step 2: Calculate the value
25 divided by 5000 equals 0.005 defects per line.Final Answer:
0.005 defects per line -> Option DQuick Check:
25 ÷ 5000 = 0.005 [OK]
- Multiplying instead of dividing
- Using wrong units or decimal places
- Confusing defect density with total defects
Solution
Step 1: Understand detection rate limits
Detection rate is defects found divided by total defects, so it must be between 0 and 1.Step 2: Check given value
Given detection rate is 1.25, which is greater than 1, so it is invalid.Final Answer:
Detection rate cannot be greater than 1 -> Option AQuick Check:
Detection rate ≤ 1 [OK]
- Allowing detection rate > 1
- Mixing detection rate with fixed defects
- Ignoring ratio limits
Solution
Step 1: Calculate total defects and defect density
Total defects = 80 found + 20 discovered = 100. Defect density = total defects / lines of code = 100 / 10,000 = 0.01.Step 2: Calculate detection rate
Detection rate = defects found / total defects = 80 / 100 = 0.8.Step 3: Match calculations to options
Defect density is 0.01 and detection rate is 0.8, but Defect density: 0.008; Detection rate: 0.8 shows defect density as 0.008 which is incorrect. Defect density: 0.01; Detection rate: 0.8 shows correct defect density but detection rate 0.8. Defect density: 0.008; Detection rate: 0.8 has defect density 0.008 which is wrong. Defect density: 0.01; Detection rate: 0.8 matches defect density 0.01 and detection rate 0.8 correctly.Final Answer:
Defect density: 0.01; Detection rate: 0.8 -> Option BQuick Check:
Defect density = 100/10000=0.01; Detection rate = 80/100=0.8 [OK]
- Using only found defects for defect density
- Ignoring defects found after release
- Mixing detection rate numerator and denominator
Ready to prove it?
Pass the test and this pattern moves unknown
