pytest -v -n auto

Automatic Process Allocation:

  • Use pytest -n auto to automatically utilize as many processes as there are physical CPU cores on your machine.

  • Alternatively, use pytest -n logical to use logical CPU cores instead. This requires the psutil package and defaults to physical cores if the logical count is unavailable.

  • You can directly specify the number of processes with a number, e.g., pytest -n 8.

  • Limit maximum workers with --maxprocesses and set restart limits with --max-worker-restart.

Modes to distribute

  • --dist load: Default mode, distributes tests to any available worker.

  • --dist loadscope: Groups tests by module or class to ensure they run in the same process.

  • --dist loadfile: Groups tests by file.

  • --dist loadgroup: Groups tests by custom group marks to ensure they run in the same worker.

  • --dist worksteal: Reassigns tests from less busy workers to idle ones to optimize fixture reuse and handle varying test durations.

  • --dist no: Normal mode, executes tests sequentially without distribution.