Introduction
When software changes, running all tests can take too long and delay feedback. Test prioritization helps decide which tests to run first to find problems quickly and save time.
Imagine you have a big pile of mail to sort, but only a little time. You decide to open urgent letters and bills first, then less important flyers later. This way, you handle the most critical mail quickly.
┌─────────────────────────────┐
│ Test Suite │
├─────────────┬───────────────┤
│ High Priority│ Low Priority │
│ Tests │ Tests │
│ (Run First) │ (Run Later) │
└─────────────┴───────────────┘
↓
Faster Bug Detection
↓
Efficient Testingtests = [
{"name": "test_login", "last_failed": True, "execution_time": 5},
{"name": "test_signup", "last_failed": False, "execution_time": 3},
{"name": "test_profile", "last_failed": True, "execution_time": 4},
{"name": "test_logout", "last_failed": False, "execution_time": 2}
]
# Prioritize tests that failed last time, then by shortest execution time
prioritized_tests = sorted(tests, key=lambda t: (not t["last_failed"], t["execution_time"]))
for test in prioritized_tests:
print(test["name"])