0
0
Testing Fundamentalstesting~10 mins

Parallel test execution in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates running two simple tests at the same time (in parallel). It verifies that both tests complete successfully without waiting for each other.

Test Code - unittest with ThreadPoolExecutor
Testing Fundamentals
import unittest
import time
from concurrent.futures import ThreadPoolExecutor

class SimpleTest(unittest.TestCase):
    def test_one(self):
        time.sleep(2)  # Simulate a delay
        self.assertEqual(1 + 1, 2)

    def test_two(self):
        time.sleep(2)  # Simulate a delay
        self.assertTrue('hello'.islower())

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(SimpleTest)
    with ThreadPoolExecutor(max_workers=2) as executor:
        futures = [executor.submit(unittest.TextTestRunner().run, test) for test in suite]
        results = [f.result() for f in futures]
    # Check all tests passed
    assert all(result.wasSuccessful() for result in results), "Some tests failed"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test suite loads two test methods: test_one and test_twoTwo test cases ready to run-PASS
2ThreadPoolExecutor starts and runs test_one and test_two in parallel threadsBoth tests running simultaneously, each sleeping for 2 seconds-PASS
3test_one completes and asserts 1 + 1 equals 2test_one finished successfullyassertEqual(1 + 1, 2)PASS
4test_two completes and asserts 'hello' is lowercasetest_two finished successfullyassertTrue('hello'.islower())PASS
5Main thread collects results from both testsBoth tests passedassert all tests passedPASS
Failure Scenario
Failing Condition: One of the tests fails its assertion or does not complete
Execution Trace Quiz - 3 Questions
Test your understanding
What does running tests in parallel help achieve?
ASkip tests that take too long
BRun tests one after another to avoid conflicts
CRun multiple tests at the same time to save time
DRun only one test to reduce errors
Key Result
Running tests in parallel can greatly reduce total test time, but each test must be independent and thread-safe to avoid conflicts.