0
0
Testing Fundamentalstesting~15 mins

Test prioritization in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Prioritize test cases based on risk and impact
Preconditions (1)
Step 1: Review the list of test cases with their risk and impact levels
Step 2: Sort the test cases so that those with highest risk and impact come first
Step 3: Select the top 3 test cases from the sorted list
Step 4: Execute the selected test cases
Step 5: Verify that the test cases executed are indeed the highest priority ones
✅ Expected Result: The test cases executed are the top 3 with the highest combined risk and impact scores
Automation Requirements - Python unittest
Assertions Needed:
Verify the test cases are sorted correctly by risk and impact
Verify only the top 3 test cases are selected for execution
Best Practices:
Use clear and descriptive variable names
Separate test data setup from test logic
Use assertions to validate sorting and selection
Keep test methods small and focused
Automated Solution
Testing Fundamentals
import unittest

class TestPrioritization(unittest.TestCase):
    def setUp(self):
        # Each test case is a dict with name, risk (1-5), impact (1-5)
        self.test_cases = [
            {'name': 'Login Test', 'risk': 5, 'impact': 4},
            {'name': 'Signup Test', 'risk': 3, 'impact': 3},
            {'name': 'Payment Test', 'risk': 4, 'impact': 5},
            {'name': 'Profile Update Test', 'risk': 2, 'impact': 2},
            {'name': 'Logout Test', 'risk': 1, 'impact': 1}
        ]

    def test_prioritize_and_select_top_three(self):
        # Sort test cases by combined risk and impact descending
        sorted_tests = sorted(
            self.test_cases,
            key=lambda x: x['risk'] + x['impact'],
            reverse=True
        )

        # Select top 3
        top_three = sorted_tests[:3]

        # Assert sorting correctness
        combined_scores = [tc['risk'] + tc['impact'] for tc in sorted_tests]
        self.assertEqual(combined_scores, sorted(combined_scores, reverse=True))

        # Assert top three length
        self.assertEqual(len(top_three), 3)

        # Assert top three are highest scoring
        max_scores = sorted(combined_scores, reverse=True)[:3]
        top_three_scores = [tc['risk'] + tc['impact'] for tc in top_three]
        self.assertEqual(top_three_scores, max_scores)

        # Print selected test case names (simulate execution)
        for test in top_three:
            print(f"Executing: {test['name']}")

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

The setUp method prepares a list of test cases with risk and impact scores.

The test method test_prioritize_and_select_top_three sorts the test cases by the sum of risk and impact in descending order. It then selects the top three test cases.

Assertions check that the sorting is correct, the top three are selected, and these top three have the highest combined scores.

Finally, it prints the names of the selected test cases to simulate execution.

This approach keeps test data separate, uses clear variable names, and validates the prioritization logic with assertions.

Common Mistakes - 4 Pitfalls
Not sorting test cases correctly by combined risk and impact
Hardcoding the top test cases without sorting
Not using assertions to verify sorting and selection
Mixing test data setup inside test logic
Bonus Challenge

Now add data-driven testing with 3 different sets of test cases having different risk and impact values

Show Hint