Skip to content

Commit 71b733d

Browse files
schlesseraclaude
andcommitted
Run scenarios in parallel workers
Setting WP_CLI_TEST_PARALLEL to a number of workers spreads the scenarios over that many Behat processes. Every worker but the first gets its own temporary directory, and with it its own home directory, caches and run directories, as well as its own test database. Scenarios relying on resources shared across the system, such as fixed ports or fixed paths in the system's temporary directory, all run in the first worker. Scenarios are distributed by their expected cost, so that the workers finish at about the same time, and the ones that fail are recorded so that rerun-behat-tests runs exactly those again. composer prepare-tests now allows the test user to create the databases of the workers. Without that permission, and on Windows, when collecting code coverage or when selecting scenarios by path or name, the scenarios still run one after the other. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b533a56 commit 71b733d

5 files changed

Lines changed: 377 additions & 0 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ composer behat -- --stop-on-failure
254254
composer behat-rerun
255255
```
256256
257+
### Running scenarios in parallel
258+
259+
Set `WP_CLI_TEST_PARALLEL` to a number of workers to spread the scenarios over that many Behat processes:
260+
```bash
261+
WP_CLI_TEST_PARALLEL=4 composer behat
262+
```
263+
264+
Every worker but the first gets its own temporary directory, and with it its own home directory, caches and run directories, as well as its own test database, named after the test database with a numeric suffix (`wp_cli_test_1`, `wp_cli_test_2`, ...). Run `composer prepare-tests` once to allow the test user to create these databases. Without that permission, the scenarios are run one after the other, as they are on Windows, when collecting code coverage, or when selecting scenarios by path or name.
265+
266+
Scenarios that rely on resources shared across the system, such as a fixed port or a fixed path in the system's temporary directory, are all run by the first worker, one after the other. `composer behat-rerun` re-runs exactly the scenarios that failed in the last parallel run.
267+
257268
### Linting the feature files
258269

259270
`composer lint-gherkin` checks `features/` with

‎bin/install-package-tests‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ install_mysql_db_8_0_plus() {
150150
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
151151
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
152152
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153+
# The databases of parallel test workers, named after the test database with a numeric suffix.
154+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153155
{ set +ex; } 2> /dev/null # stop printing the commands
154156
}
155157

@@ -159,6 +161,8 @@ install_mysql_db_lower_than_8_0() {
159161
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
160162
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
161163
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
164+
# The databases of parallel test workers, named after the test database with a numeric suffix.
165+
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
162166
{ set +ex; } 2> /dev/null # stop printing the commands
163167
}
164168

@@ -169,6 +173,8 @@ install_mariadb() {
169173
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
170174
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
171175
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
176+
# The databases of parallel test workers, named after the test database with a numeric suffix.
177+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
172178
}
173179

174180
if [ "${TYPE}" = "MariaDB" ]; then

‎bin/run-behat-tests‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,26 @@ for arg in "$@"; do
270270
fi
271271
done
272272

273+
# Run the scenarios in parallel workers when WP_CLI_TEST_PARALLEL is set to their number.
274+
# Every worker but the first uses its own test database, which the test user needs
275+
# to be allowed to create, as set up by "composer prepare-tests".
276+
PARALLEL="${WP_CLI_TEST_PARALLEL-}"
277+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
278+
if ! is_numeric "${PARALLEL}"; then
279+
echo "Warning: WP_CLI_TEST_PARALLEL is not a number of workers, running the scenarios one after the other." >&2
280+
PARALLEL=""
281+
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
282+
echo "Code coverage is collected, running the scenarios one after the other." >&2
283+
PARALLEL=""
284+
elif [ "${WP_CLI_TEST_DBTYPE-}" != "sqlite" ] && ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="CREATE DATABASE IF NOT EXISTS \`${DBNAME}_1\`; DROP DATABASE \`${DBNAME}_1\`;" 2>/dev/null; then
285+
echo "Warning: The test user cannot create the databases of parallel workers, running the scenarios one after the other." >&2
286+
echo "Run 'composer prepare-tests' once to allow it." >&2
287+
PARALLEL=""
288+
fi
289+
fi
290+
291+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
292+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php "${PARALLEL}" --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
293+
fi
294+
273295
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"

‎phpcs.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@
7979
so this file does not have to comply with WP naming conventions. -->
8080
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
8181
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
82+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8283
</rule>
8384
<rule ref="WordPress.WP.GlobalVariablesOverride">
8485
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
86+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8587
</rule>
8688

8789
<!-- This is a procedural stand-alone file that is adding polyfills when

0 commit comments

Comments
 (0)