Introduction
Imagine running the same test multiple times and getting different results each time. This unpredictability makes it hard to trust test outcomes and slows down development. Managing flaky tests helps keep testing reliable and efficient.
Imagine a smoke alarm that sometimes goes off without smoke and sometimes stays silent when there is smoke. This unreliable alarm causes people to ignore it or waste time checking for fires that aren't there. Fixing or managing this alarm is crucial for safety and trust.
┌─────────────────────────────┐ │ Flaky Test Suite │ ├─────────────┬───────────────┤ │ Causes │ Management │ │ ┌─────────┐ │ ┌───────────┐ │ │ │ Timing │ │ │ Isolation │ │ │ │ Issues │ │ │ & Mocking │ │ │ ├─────────┤ │ ├───────────┤ │ │ │ External│ │ │ Retries │ │ │ │ Factors │ │ │ Quarantine│ │ │ └─────────┘ │ └───────────┘ │ └─────────────┴───────────────┘
import unittest import random class FlakyTestExample(unittest.TestCase): def test_flaky_behavior(self): # Simulate a flaky test by randomly passing or failing self.assertTrue(random.choice([True, False])) if __name__ == '__main__': unittest.main()