Test prioritization in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of test cases having different risk and impact values
Practice
test prioritization in software testing?Solution
Step 1: Understand the purpose of test prioritization
Test prioritization means deciding which tests to run first based on their importance or likelihood to find bugs.Step 2: Identify the correct goal
The goal is to save time and find bugs faster by running important tests first.Final Answer:
To run the most important tests first to find bugs faster -> Option AQuick Check:
Test prioritization = run important tests first [OK]
- Thinking test order is random
- Believing all tests have equal priority
- Skipping tests without reason
Solution
Step 1: Identify relevant factors for test prioritization
Good factors include recent code changes, past failures, and test importance.Step 2: Choose the factor related to code changes
Recent changes in code can introduce new bugs, so tests covering those areas should run first.Final Answer:
Recent changes in the code -> Option DQuick Check:
Prioritize tests by recent code changes [OK]
- Choosing unrelated factors like document color
- Ignoring recent code updates
- Confusing tester count with test priority
Test A: failed 2 days ago Test B: passed last run Test C: failed 10 days ago Test D: never failed
Solution
Step 1: Analyze failure recency
Tests that failed recently are more likely to find bugs again, so they get higher priority.Step 2: Compare failure dates
Test A failed 2 days ago, which is more recent than Test C (10 days ago). Tests B and D did not fail recently.Final Answer:
Test A -> Option CQuick Check:
Run tests with recent failures first [OK]
- Choosing tests that never failed
- Ignoring failure dates
- Picking tests that passed last run
1. Test X (no recent changes, no failures) 2. Test Y (recent changes, no failures) 3. Test Z (no recent changes, failed last run) 4. Test W (recent changes, failed last run)
Which test should be run first to follow best test prioritization?
Solution
Step 1: Combine recent changes and past failures
Tests with both recent changes and past failures have highest priority.Step 2: Identify test with both factors
Test W has recent changes and failed last run, so it should run first.Final Answer:
Test W -> Option AQuick Check:
Prioritize tests with recent changes and failures [OK]
- Ignoring one of the prioritization factors
- Running tests without recent changes first
- Choosing tests that never failed
Test 1: covers critical feature, no recent changes, failed last run Test 2: covers minor feature, recent changes, passed last run Test 3: covers critical feature, recent changes, passed last run Test 4: covers minor feature, no recent changes, never failed Test 5: covers critical feature, recent changes, failed last run
Which order best follows test prioritization principles?
Solution
Step 1: Identify highest priority tests
Tests covering critical features with recent changes and failures have highest priority. Test 5 fits all three.Step 2: Order remaining tests by priority
Next is Test 1 (critical, failed but no recent changes), then Test 3 (critical, recent changes, passed), followed by Test 2 and Test 4 (minor features, lower priority).Final Answer:
Test 5, Test 1, Test 3, Test 2, Test 4 -> Option BQuick Check:
Prioritize critical, recent changes, failures first [OK]
- Ignoring feature criticality
- Not combining multiple factors
- Running low priority tests first
Ready to prove it?
Pass the test and this pattern moves unknown
