Skip to content
Latchkey

xargs -P: Run Test Shards in Parallel

xargs -P runs several test commands at once, sharding your test files across CPU cores.

If your test runner does not parallelize itself, xargs -P can split test files into concurrent groups and run them across all cores on the runner.

What it does

By listing test files and piping them into xargs with -P and -n, each worker runs a batch of tests concurrently. xargs waits for all workers and exits non-zero if any worker failed, so a single failing shard fails the CI step. Group with -n to amortize process startup, or -I {} for one file per run.

Common usage

Terminal
# run all spec files, 4 at a time, batches of 10
find test -name '*.spec.js' -print0 \
  | xargs -0 -P 4 -n 10 node --test
# one runner per file, parallel across cores
find test -name '*_test.py' -print0 \
  | xargs -0 -P "$(nproc)" -I {} pytest {}

Options

FlagWhat it does
-P <n>Number of parallel test workers
-n <n>Test files per worker invocation
-I {}One test file per invocation
-0 (with -print0)Handle test paths with spaces safely

In CI

Match -P to nproc, but watch shared resources: parallel tests hitting one database or port collide. Give each worker its own temp dir or DB schema. Because xargs propagates a non-zero exit when any shard fails, the step fails correctly even though output from shards is interleaved.

Common errors in CI

Flaky failures under -P that vanish when serial usually mean shared state (a fixed port, a single test DB). Interleaved, hard-to-read logs are expected; have each worker write a separate report file. If the step passes despite a failing test, the runner may swallow the exit code; confirm xargs is the last command in the pipe.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →