You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Running Tests In Parallel](#running-tests-in-parallel)
7
8
8
9
<aname="introduction"></a>
9
10
## Introduction
@@ -19,7 +20,7 @@ An `ExampleTest.php` file is provided in both the `Feature` and `Unit` test dire
19
20
<aname="environment"></a>
20
21
## Environment
21
22
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.
23
24
24
25
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!
25
26
@@ -28,8 +29,8 @@ You are free to define other testing environment configuration values as necessa
28
29
29
30
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.
30
31
31
-
<aname="creating-and-running-tests"></a>
32
-
## Creating & Running Tests
32
+
<aname="creating-tests"></a>
33
+
## Creating Tests
33
34
34
35
To create a new test case, use the `make:test` Artisan command. By default, tests will be placed in the `tests/Feature` directory:
35
36
@@ -64,8 +65,12 @@ Once the test has been generated, you may define test methods as you normally wo
64
65
65
66
> {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.
66
67
67
-
<aname="artisan-test-runner"></a>
68
-
### Artisan Test Runner
68
+
<aname="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
69
74
70
75
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:
71
76
@@ -74,3 +79,69 @@ In addition to the `phpunit` command, you may use the `test` Artisan command to
74
79
Any arguments that can be passed to the `phpunit` command may also be passed to the Artisan `test` command:
75
80
76
81
php artisan test --testsuite=Feature --stop-on-failure
82
+
83
+
84
+
<aname="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
+
<aname="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
+
<aname="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:
0 commit comments