Skip to content

Commit 1fe56a3

Browse files
[9.x] Adds parallel testing documentation (#6737)
* Adds parallel testing documention * Wording * formatting and additions to parallel testing docs Co-authored-by: Taylor Otwell <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5f84a3a commit 1fe56a3

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

testing.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
- [Introduction](#introduction)
44
- [Environment](#environment)
5-
- [Creating & Running Tests](#creating-and-running-tests)
6-
- [Artisan Test Runner](#artisan-test-runner)
5+
- [Creating Tests](#creating-tests)
6+
- [Running Tests](#running-tests)
7+
- [Running Tests In Parallel](#running-tests-in-parallel)
78

89
<a name="introduction"></a>
910
## Introduction
@@ -19,7 +20,7 @@ An `ExampleTest.php` file is provided in both the `Feature` and `Unit` test dire
1920
<a name="environment"></a>
2021
## Environment
2122

22-
When running tests via `vendor/bin/phpunit`, Laravel will automatically set the [configuration environment](/docs/{{version}}/configuration#environment-configuration) to `testing` because of the environment variables defined in the `phpunit.xml` file. Laravel also automatically configures the session and cache to the `array` driver while testing, meaning no session or cache data will be persisted while testing.
23+
When running tests, Laravel will automatically set the configuration environment to `testing` because of the environment variables defined in the `phpunit.xml` file. Laravel also automatically configures the session and cache to the `array` driver while testing, meaning no session or cache data will be persisted while testing.
2324

2425
You are free to define other testing environment configuration values as necessary. The `testing` environment variables may be configured in your application's `phpunit.xml` file, but make sure to clear your configuration cache using the `config:clear` Artisan command before running your tests!
2526

@@ -28,8 +29,8 @@ You are free to define other testing environment configuration values as necessa
2829

2930
In addition, you may create a `.env.testing` file in the root of your project. This file will be used instead of the `.env` file when running PHPUnit tests or executing Artisan commands with the `--env=testing` option.
3031

31-
<a name="creating-and-running-tests"></a>
32-
## Creating & Running Tests
32+
<a name="creating-tests"></a>
33+
## Creating Tests
3334

3435
To create a new test case, use the `make:test` Artisan command. By default, tests will be placed in the `tests/Feature` directory:
3536

@@ -64,8 +65,12 @@ Once the test has been generated, you may define test methods as you normally wo
6465

6566
> {note} If you define your own `setUp` / `tearDown` methods within a test class, be sure to call the respective `parent::setUp()` / `parent::tearDown()` methods on the parent class.
6667
67-
<a name="artisan-test-runner"></a>
68-
### Artisan Test Runner
68+
<a name="running-tests"></a>
69+
## Running Tests
70+
71+
As mentioned previously, once you've written tests, you may run them using `phpunit`:
72+
73+
./vendor/bin/phpunit
6974

7075
In addition to the `phpunit` command, you may use the `test` Artisan command to run your tests. The Artisan test runner provides verbose test reports in order to ease development and debugging:
7176

@@ -74,3 +79,69 @@ In addition to the `phpunit` command, you may use the `test` Artisan command to
7479
Any arguments that can be passed to the `phpunit` command may also be passed to the Artisan `test` command:
7580

7681
php artisan test --testsuite=Feature --stop-on-failure
82+
83+
84+
<a name="running-tests-in-parallel"></a>
85+
### Running Tests In Parallel
86+
87+
By default, Laravel and PHPUnit execute your tests sequentially within a single process. However, you may greatly reduce the amount of time it takes to run your tests by running tests simultaneously across multiple processes. To get started, include the `--parallel` option when executing the `test` Artisan command:
88+
89+
php artisan test --parallel
90+
91+
By default, Laravel will create as many processes as there are available CPU cores on your machine. However, you may adjust the number of processes using the `--processes` option:
92+
93+
php artisan test --parallel --processes=4
94+
95+
<a name="parallel-testing-and-databases"></a>
96+
#### Parallel Testing & Databases
97+
98+
Laravel automatically handles creating and migrating a test database for each parallel process that is running your tests. The test databases will be suffixed with a process token which is unique per process. For example, if you have two parallel test processes, Laravel will create and use `your_db_test_1` and `your_db_test_2` test databases.
99+
100+
By default, test databases persist between calls to the `test` Artisan command so that they can be used again by subsequent `test` invocations. However, you may re-create them using the `--recreate-databases` option:
101+
102+
php artisan test --parallel --recreate-databases
103+
104+
<a name="parallel-testing-hooks"></a>
105+
#### Parallel Testing Hooks
106+
107+
Occasionally, you may need to prepare certain resources used by your application's tests so they may be safely used by multiple test processes.
108+
109+
Using the `ParallelTesting` facade, you may specify code to be executed on the `setUp` and `tearDown` of a process or test case. The given closures receive the `$token` and `$testCase` variables that contain the process token and the current test case, respectively:
110+
111+
<?php
112+
113+
namespace App\Providers;
114+
115+
use Illuminate\Support\Facades\ParallelTesting;
116+
use Illuminate\Support\ServiceProvider;
117+
118+
class AppServiceProvider extends ServiceProvider
119+
{
120+
/**
121+
* Bootstrap any application services.
122+
*
123+
* @return void
124+
*/
125+
public function boot()
126+
{
127+
ParallelTesting::setUpProcess(function ($token) {
128+
// ..
129+
});
130+
131+
ParallelTesting::setUpTestCase(function ($token, $testCase) {
132+
// ..
133+
});
134+
135+
ParallelTesting::tearDownTestCase(function ($token, $testCase) {
136+
// ..
137+
});
138+
139+
ParallelTesting::tearDownProcess(function ($token) {
140+
// ..
141+
});
142+
}
143+
}
144+
145+
If you would like to access to current process token from any other place in your application's test code, you may use the `token` method:
146+
147+
$token = ParallelTesting::token();

0 commit comments

Comments
 (0)