liveBook · Manning

Parallel test execution

As explained in The Art of Unit Testing 3e

Parallel test execution is the concurrent execution of multiple tests to reduce total test execution time.

Parallel test execution runs multiple tests or test groups concurrently, reducing the time developers wait for feedback. It can happen inside a test runner, across test layers, or across pipeline stages. section 2.6 section 10.4.2

Speed comes with a trustworthiness risk: shared, stateful setup can be overwritten by another test running at the same time. Forgotten resets and shared variables can therefore create flaky failures that are difficult to diagnose. section 2.6

A shared verifier can become unsafe

A Jest suite may create one higher-level verifier in an outer beforeEach() and add a fake rule in an inner beforeEach(). This shortens the tests, but parallel execution makes shared state risky. Create isolated state for each test and reset mutable data such as the errors array. section 2.6

Use parallel execution deliberately:

  • Run independent test layers in parallel to shorten pipeline feedback. section 10.4.2
  • Split a large end-to-end test collection into parallel suites. section 10.4.2
  • Use dynamically created environments when parallel tests need separate execution space. section 10.4.2
  • Run the delivery pipeline after each commit instead of waiting for a nightly build. section 10.4.2
Listing 2.13 Using beforeEach() on two levels
describe('PasswordVerifier', () => {
  let verifier;
  beforeEach(() => verifier = new PasswordVerifier1());   #1
  describe('with a failing rule', () => {
    let fakeRule, errors;
    beforeEach(() => {                                    #2
      fakeRule = input => ({passed: false, reason: 'fake reason'});
      verifier.addRule(fakeRule);
    });
    it('has an error message based on the rule.reason', () => {
      const errors = verifier.verify('any value');
 
      expect(errors[0]).toContain('fake reason');
    });
    it('has exactly one error', () => {
      const errors = verifier.verify('any value');
 
      expect(errors.length).toBe(1);
    });
  });
});
#1 Setting up a new verifier that will be used in each test
#2 Setting up a fake rule that will be used within this describe() method

Inspect the nested beforeEach() example showing shared verifier setup and scenario-specific fake rules. section 2.6

Figure 10.7 To speed up delivery, you can run pipelines, and even stages in pipelines, in parallel.
10-07

Inspect the figure showing pipelines and pipeline stages running in parallel to accelerate delivery feedback. section 10.4.2

How The Art of Unit Testing 3e covers it

How deep it goes
Descends from execution patterns to shared-state failure modes, but stops short of detailed runner configuration.
Examples
Uses Jest beforeEach() setup, a shared verifier, fake validation rules, parallel E2E suites, and dynamic environments.
What you can do after
Operate/configure parallel tests and pipelines while isolating mutable state and reducing feedback delays.
How it connects
Connects unit tests, test layers, pipelines, end-to-end tests, dynamic environments, and test isolation.

Also explained in

Compare how 2 books explain parallel test execution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.