Chris@0
|
1 # Running tests
|
Chris@0
|
2
|
Chris@18
|
3 ## Setting up
|
Chris@0
|
4
|
Chris@18
|
5 ### PHP dependencies
|
Chris@0
|
6
|
Chris@18
|
7 You need the Drupal core development dependencies installed, in order to run
|
Chris@18
|
8 any tests. You can install them using Composer by running
|
Chris@18
|
9 ```
|
Chris@18
|
10 composer install
|
Chris@18
|
11 ```
|
Chris@18
|
12 in the Drupal root directory. These dependencies should not be installed on a
|
Chris@18
|
13 production site.
|
Chris@0
|
14
|
Chris@18
|
15 ### Test directory
|
Chris@17
|
16
|
Chris@18
|
17 Create a directory called sites/simpletest and make sure that it is writable by
|
Chris@18
|
18 the web server and/or all users.
|
Chris@17
|
19
|
Chris@18
|
20 ### PHPUnit configuration
|
Chris@0
|
21
|
Chris@18
|
22 Copy the core/phpunit.xml.dist file to phpunit.xml, and place it somewhere
|
Chris@18
|
23 convenient (inside the core directory may not be the best spot, since that
|
Chris@18
|
24 directory may be managed by Composer or Git). You can use the -c option on the
|
Chris@18
|
25 command line to tell PHPUnit where this file is (use the full path).
|
Chris@0
|
26
|
Chris@18
|
27 Settings to change in this file:
|
Chris@18
|
28 * SIMPLETEST_BASE_URL: The URL of your site
|
Chris@18
|
29 * SIMPLETEST_DB: The URL of your Drupal database
|
Chris@18
|
30 * The bootstrap attribute of the top-level phpunit tag, to take into account
|
Chris@18
|
31 the location of the file
|
Chris@18
|
32 * BROWSERTEST_OUTPUT_DIRECTORY: Set to sites/simpletest/browser_output;
|
Chris@18
|
33 you will also want to uncomment the printerClass attribute of the
|
Chris@18
|
34 top-level phpunit tag.
|
Chris@18
|
35
|
Chris@18
|
36 ### Additional setup for JavaScript tests
|
Chris@18
|
37
|
Chris@18
|
38 To run JavaScript tests based on the
|
Chris@18
|
39 \Drupal\FunctionalJavascriptTests\WebDriverTestBase base class, you will need
|
Chris@18
|
40 to install the following additional software:
|
Chris@18
|
41
|
Chris@18
|
42 * Google Chrome or Chromium browser
|
Chris@18
|
43 * chromedriver (tested with version 2.45) -- see
|
Chris@18
|
44 https://sites.google.com/a/chromium.org/chromedriver/
|
Chris@18
|
45 * PHP 7.1 or higher
|
Chris@18
|
46
|
Chris@18
|
47 ## Running unit, functional, and kernel tests
|
Chris@18
|
48
|
Chris@18
|
49 The PHPUnit executable is vendor/bin/phpunit -- you will need to locate your
|
Chris@18
|
50 vendor directory (which may be outside the Drupal root).
|
Chris@18
|
51
|
Chris@18
|
52 Here are commands to run one test class, list groups, and run all the tests in
|
Chris@18
|
53 a particular group:
|
Chris@0
|
54 ```
|
Chris@18
|
55 ./vendor/bin/phpunit -c /path/to/your/phpunit.xml path/to/your/class/file.php
|
Chris@18
|
56 ./vendor/bin/phpunit --list-groups
|
Chris@18
|
57 ./vendor/bin/phpunit -c /path/to/your/phpunit.xml --group Groupname
|
Chris@0
|
58 ```
|
Chris@0
|
59
|
Chris@18
|
60 More information on running tests can be found at
|
Chris@18
|
61 https://www.drupal.org/docs/8/phpunit/running-phpunit-tests
|
Chris@0
|
62
|
Chris@18
|
63 ## Running Functional JavaScript tests
|
Chris@18
|
64
|
Chris@18
|
65 You can run JavaScript tests that are based on the
|
Chris@18
|
66 \Drupal\FunctionalJavascriptTests\WebDriverTestBase base class in the same way
|
Chris@18
|
67 as other PHPUnit tests, except that before you start, you will need to start
|
Chris@18
|
68 chromedriver using port 4444, and keep it running:
|
Chris@0
|
69 ```
|
Chris@18
|
70 /path/to/chromedriver --port=4444
|
Chris@0
|
71 ```
|
Chris@0
|
72
|
Chris@18
|
73 ## Running Nightwatch tests
|
Chris@14
|
74
|
Chris@18
|
75 * Ensure your vendor directory is populated
|
Chris@18
|
76 (e.g. by running `composer install`)
|
Chris@18
|
77 * If you're running PHP 7.0 or greater you will need to upgrade PHPUnit with
|
Chris@18
|
78 `composer run-script drupal-phpunit-upgrade`
|
Chris@18
|
79 * Install [Node.js](https://nodejs.org/en/download/) and
|
Chris@18
|
80 [yarn](https://yarnpkg.com/en/docs/install). The versions required are
|
Chris@18
|
81 specificed inside core/package.json in the `engines` field
|
Chris@18
|
82 * Install
|
Chris@18
|
83 [Google Chrome](https://www.google.com/chrome/browser/desktop/index.html)
|
Chris@18
|
84 * Inside the `core` folder, run `yarn install`
|
Chris@18
|
85 * Configure the nightwatch settings by copying `.env.example` to `.env` and
|
Chris@18
|
86 editing as necessary.
|
Chris@18
|
87 * Ensure you have a web server running (as instructed in `.env`)
|
Chris@18
|
88 * Again inside the `core` folder, run `yarn test:nightwatch` to run the tests.
|
Chris@18
|
89 By default this will output reports to `core/reports`
|
Chris@18
|
90 * Nightwatch will run tests for core, as well as contrib and custom modules and
|
Chris@18
|
91 themes. It will search for tests located under folders with the pattern
|
Chris@18
|
92 `**/tests/**/Nightwatch/(Tests|Commands|Assertions)`
|
Chris@18
|
93 * To run only core tests, run `yarn test:nightwatch --tag core`
|
Chris@18
|
94 * To skip running core tests, run `yarn test:nightwatch --skiptags core`
|
Chris@18
|
95 * To run a single test, run e.g.
|
Chris@18
|
96 `yarn test:nightwatch tests/Drupal/Nightwatch/Tests/exampleTest.js`
|
Chris@14
|
97
|
Chris@18
|
98 Nightwatch tests, as well as custom commands, assertions and pages, can be
|
Chris@18
|
99 placed in any folder with the pattern
|
Chris@18
|
100 `**/tests/**/Nightwatch/(Tests|Commands|Assertions|Pages)`. For example:
|
Chris@17
|
101 ```
|
Chris@17
|
102 tests/Nightwatch/Tests
|
Chris@17
|
103 src/tests/Nightwatch/Tests
|
Chris@17
|
104 tests/src/Nightwatch/Tests
|
Chris@17
|
105 tests/Nightwatch/Commands
|
Chris@18
|
106 tests/src/Nightwatch/Assertions
|
Chris@18
|
107 tests/src/Nightwatch/Pages
|
Chris@17
|
108 ```
|
Chris@17
|
109
|
Chris@18
|
110 It's helpful to follow existing patterns for test placement, so for the action
|
Chris@18
|
111 module they would go in `core/modules/action/tests/src/Nightwatch`.
|
Chris@18
|
112 The Nightwatch configuration, as well as global tests, commands, and assertions
|
Chris@18
|
113 which span many modules/systems, are located in `core/tests/Drupal/Nightwatch`.
|
Chris@17
|
114
|
Chris@18
|
115 If your core directory is located in a subfolder (e.g. `docroot`), then you can
|
Chris@18
|
116 edit the search directory in `.env` to pick up tests outside of your Drupal
|
Chris@18
|
117 directory. Tests outside of the `core` folder will run in the version of node
|
Chris@18
|
118 you have installed. If you want to transpile with babel (e.g. to use `import`
|
Chris@18
|
119 statements) outside of core, then add your own babel config to the root of your
|
Chris@18
|
120 project. For example, if core is located under `docroot/core`, then you could
|
Chris@18
|
121 run `yarn add babel-preset-env` inside `docroot`, then copy the babel settings
|
Chris@18
|
122 from `docroot/core/package.json` into `docroot/package.json`.
|
Chris@18
|
123
|
Chris@18
|
124 ## Troubleshooting test running
|
Chris@18
|
125
|
Chris@18
|
126 If you run into file permission problems while running tests, you may need to
|
Chris@18
|
127 invoke the phpunit executable with a user in the same group as the web server
|
Chris@18
|
128 user, or with access to files owned by the web server user. For example:
|
Chris@18
|
129 ```
|
Chris@18
|
130 sudo -u www-data ./vendor/bin/phpunit -c /path/to/your/phpunit.xml --group Groupname
|
Chris@18
|
131 ```
|
Chris@18
|
132
|
Chris@18
|
133 If you have permission problems accessing files after running tests, try
|
Chris@18
|
134 putting
|
Chris@18
|
135 ```
|
Chris@18
|
136 $settings['file_chmod_directory'] = 02775;
|
Chris@18
|
137 ```
|
Chris@18
|
138 in your settings.php or local.settings.php file.
|
Chris@18
|
139
|
Chris@18
|
140 You may need to use absolute paths in your phpunit.xml file, and/or in your
|
Chris@18
|
141 phpunit command arguments.
|