Welcome to our deep dive into Testing Metrics! This lesson is designed to help you understand and apply essential testing metrics in your software engineering journey. Let's get started!
Testing metrics provide insights into the quality of your codebase, helping you to identify areas of improvement and optimize your testing process. In this lesson, we'll explore various testing metrics, their importance, and how to calculate them.
Coverage metrics help you ensure that your tests are exercising all the parts of your codebase. Here are the two most common coverage metrics:
Calculate CC by dividing the number of executed lines by the total number of lines in your codebase.
Example (Python):
import unittest
class TestExample(unittest.TestCase):
def test_addition(self):
self.assertEqual(add(1, 2), 3)
def add(a, b):
return a + b
if __name__ == "__main__":
unittest.main()Running the above code snippet will give you a CC of 100%.
BC measures the percentage of decision points in your code that are being tested. To calculate BC, divide the number of executed decision points by the total number of decision points in your codebase.
In the example below, we'll add an if statement to our previous example:
import unittest
class TestExample(unittest.TestCase):
def test_addition(self):
self.assertEqual(add(1, 2), 3)
self.assertEqual(add(3, 4), 7)
self.assertEqual(add(2, 2), 4) # This line covers the if statement
def test_subtraction(self):
self.assertEqual(subtract(4, 2), 2)
def add(a, b):
if a > b:
return a + b
else:
return a - b
def subtract(a, b):
return a - b
if __name__ == "__main__":
unittest.main()Now, if you run this code, the BC will be greater than 0%.
What is the purpose of Code Coverage (CC)?
Defect Density measures the number of defects per unit of code. It helps you identify areas of your codebase that require improvement. To calculate Defect Density (DD), divide the number of defects found in a certain period by the total lines of code.
Example:
Test Effort quantifies the resources required for testing. It includes the time spent on writing, executing, and maintaining tests. Test Effort can be reduced by automating tests and focusing on high-risk areas of the codebase.
Test Effectiveness measures the ability of tests to detect defects. It can be improved by focusing on high-risk areas of the codebase, adding more tests, and ensuring good code coverage.
And there you have it! Now you have a solid understanding of testing metrics and how to apply them in your software engineering journey. Happy coding! 🎉