Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

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

Choose your learning style10 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

Practice

(1/5)
1. What does defect density measure in software testing?
easy
A. Percentage of passed test cases
B. Total number of test cases executed
C. Number of defects per size of software
D. Time taken to fix defects

Solution

  1. 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).
  2. 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.
  3. Final Answer:

    Number of defects per size of software -> Option C
  4. Quick Check:

    Defect density = Defects / Size [OK]
Hint: Defect density = defects divided by software size [OK]
Common Mistakes:
  • Confusing defect density with total defects
  • Mixing defect density with test case count
  • Thinking defect density measures fix time
2. Which formula correctly calculates the detection rate during testing?
easy
A. (Number of defects found) / (Total defects present)
B. (Total defects present) / (Number of defects found)
C. (Number of test cases passed) / (Total test cases)
D. (Number of defects fixed) / (Number of defects found)

Solution

  1. Step 1: Recall detection rate meaning

    Detection rate measures how many defects testing finds out of all defects present.
  2. 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.
  3. Final Answer:

    (Number of defects found) / (Total defects present) -> Option A
  4. Quick Check:

    Detection rate = Found / Total [OK]
Hint: Detection rate = defects found ÷ total defects [OK]
Common Mistakes:
  • Reversing numerator and denominator
  • Confusing detection rate with pass rate
  • Using fixed defects instead of found defects
3. A software module has 5000 lines of code and 25 defects found. What is the defect density?
medium
A. 0.5 defects per line
B. 0.05 defects per line
C. 2 defects per line
D. 0.005 defects per line

Solution

  1. Step 1: Apply defect density formula

    Defect density = Number of defects / Size of software = 25 / 5000.
  2. Step 2: Calculate the value

    25 divided by 5000 equals 0.005 defects per line.
  3. Final Answer:

    0.005 defects per line -> Option D
  4. Quick Check:

    25 ÷ 5000 = 0.005 [OK]
Hint: Divide defects by lines of code for defect density [OK]
Common Mistakes:
  • Multiplying instead of dividing
  • Using wrong units or decimal places
  • Confusing defect density with total defects
4. During testing, 40 defects were found but the total defects present were 50. The detection rate was calculated as 1.25. What is the error?
medium
A. Detection rate cannot be greater than 1
B. Detection rate should be less than 0.5
C. Detection rate equals defects fixed, not found
D. Detection rate is unrelated to defects

Solution

  1. Step 1: Understand detection rate limits

    Detection rate is defects found divided by total defects, so it must be between 0 and 1.
  2. Step 2: Check given value

    Given detection rate is 1.25, which is greater than 1, so it is invalid.
  3. Final Answer:

    Detection rate cannot be greater than 1 -> Option A
  4. Quick Check:

    Detection rate ≤ 1 [OK]
Hint: Detection rate max is 1 (100%) [OK]
Common Mistakes:
  • Allowing detection rate > 1
  • Mixing detection rate with fixed defects
  • Ignoring ratio limits
5. A project has 10,000 lines of code with 80 defects found during testing. Later, 20 more defects were discovered after release. What is the updated defect density and detection rate?
hard
A. Defect density: 0.008; Detection rate: 1.0
B. Defect density: 0.01; Detection rate: 0.8
C. Defect density: 0.01; Detection rate: 1.0
D. Defect density: 0.008; Detection rate: 0.8

Solution

  1. 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.
  2. Step 2: Calculate detection rate

    Detection rate = defects found / total defects = 80 / 100 = 0.8.
  3. 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.
  4. Final Answer:

    Defect density: 0.01; Detection rate: 0.8 -> Option B
  5. Quick Check:

    Defect density = 100/10000=0.01; Detection rate = 80/100=0.8 [OK]
Hint: Add all defects for density; found ÷ total for detection rate [OK]
Common Mistakes:
  • Using only found defects for defect density
  • Ignoring defects found after release
  • Mixing detection rate numerator and denominator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard