0
0
Testing Fundamentalstesting~15 mins

Traceability matrix in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify Traceability Matrix Links Requirements to Test Cases
Preconditions (3)
Step 1: Open the traceability matrix document
Step 2: For each requirement listed, find the corresponding test cases linked
Step 3: Verify that each requirement has at least one linked test case
Step 4: Verify that no test case is linked to a non-existent requirement
Step 5: Check that the traceability matrix is complete and up to date
✅ Expected Result: Each requirement is linked to one or more valid test cases, and all test cases correspond to existing requirements. The matrix is complete and accurate.
Automation Requirements - Python unittest
Assertions Needed:
Assert that every requirement has at least one linked test case
Assert that all linked test cases correspond to existing requirements
Assert that no requirement or test case is missing from the matrix
Best Practices:
Use data-driven testing to iterate over requirements and test cases
Use clear and descriptive assertion messages
Structure code for readability and maintainability
Automated Solution
Testing Fundamentals
import unittest

class TraceabilityMatrixTest(unittest.TestCase):
    def setUp(self):
        # Sample data simulating the traceability matrix
        self.requirements = ['REQ-001', 'REQ-002', 'REQ-003']
        self.test_cases = ['TC-101', 'TC-102', 'TC-103', 'TC-104']
        # Mapping from requirements to test cases
        self.matrix = {
            'REQ-001': ['TC-101', 'TC-102'],
            'REQ-002': ['TC-103'],
            'REQ-003': ['TC-104']
        }

    def test_requirements_have_test_cases(self):
        for req in self.requirements:
            with self.subTest(requirement=req):
                self.assertIn(req, self.matrix, f"Requirement {req} missing in matrix")
                self.assertTrue(len(self.matrix[req]) > 0, f"Requirement {req} has no linked test cases")

    def test_test_cases_linked_to_existing_requirements(self):
        all_linked_test_cases = []
        for req, tcs in self.matrix.items():
            for tc in tcs:
                all_linked_test_cases.append(tc)
        for tc in all_linked_test_cases:
            self.assertIn(tc, self.test_cases, f"Test case {tc} linked but does not exist")

    def test_no_missing_requirements_or_test_cases(self):
        # Check all requirements in matrix are known
        for req in self.matrix.keys():
            self.assertIn(req, self.requirements, f"Matrix has unknown requirement {req}")
        # Check all test cases in matrix are known
        for tcs in self.matrix.values():
            for tc in tcs:
                self.assertIn(tc, self.test_cases, f"Matrix has unknown test case {tc}")

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

This test script uses Python's unittest framework to automate verification of a traceability matrix.

setUp() prepares sample data representing requirements, test cases, and their links.

test_requirements_have_test_cases checks each requirement has linked test cases.

test_test_cases_linked_to_existing_requirements verifies all linked test cases exist in the test case list.

test_no_missing_requirements_or_test_cases ensures the matrix does not contain unknown requirements or test cases.

Assertions include clear messages to help identify issues.

This structure makes the test easy to read and maintain.

Common Mistakes - 3 Pitfalls
Not verifying that each requirement has at least one test case linked
Hardcoding test cases without checking if they exist in the test case list
Ignoring unknown requirements or test cases in the matrix
Bonus Challenge

Now add data-driven testing with 3 different sets of requirements and test cases to verify the matrix for each set.

Show Hint