# Optimizing Tests Pest offers several optimization techniques to help developers write efficient and high-performing tests. One of the most important is parallel testing, which allows multiple tests to run simultaneously across multiple processes using the `--parallel` option. This can greatly reduce the time it takes to run tests and improve the overall performance of your test suite. Additionally, Pest provides the `--profile` flag to quickly identify slow-running tests, allowing you to optimize their execution. Finally, it's often useful to focus solely on your test suite's failures. To do this, you can utilize the `--compact` printer when running Pest. This instructs Pest to only display information regarding your test suite's failing tests. ## Parallel Testing By default, Pest executes your tests sequentially within a single process. However, you can significantly decrease the time needed to run your tests by using the --parallel option to run them concurrently across multiple processes. If you’re running on Windows, be sure to use a WSL terminal. ```bash ./vendor/bin/pest --parallel ``` When running tests in parallel, Pest will create a process for each available CPU core on your machine. However, you can manually modify the number of processes using the `--processes` option. ```bash ./vendor/bin/pest --parallel --processes=10 ``` Here are some important points to consider when writing tests that will be executed in parallel: 1. **Database resources may not be shared between tests**: Each test should be isolated and independent from other tests. 2. **Test order may not be guaranteed**: Tests should not rely on any specific order of execution. 3. **Tests may be affected by race conditions**: Race conditions can occur when multiple processes or threads are accessing shared resources. Make sure to design your tests to handle potential race conditions and avoid them whenever possible. ## Profiling Imagine you have a large test suite that takes several minutes to run. You've noticed that some tests take significantly longer than others, but you're not sure which tests are the slowest or what's causing the slowdown. To identify the slowest tests and optimize their execution, you can use Pest's `--profile` option. When you run your test suite with this flag enabled, Pest will collect the duration of each test and provide a report that highlights the slowest tests. ```bash ./vendor/bin/pest --profile ``` For example, imagine you run your test suite and see the following output: