So I am trying to create some unit tests in WordPress and I installed PHPUnit 6.5.5(tried various versions up to 7.* which is the latest supported by WP).
The generated phpunit.xml.dist file looks like this:
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="default">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
The tests folder is the default one with bootstrap.php and test-sample.php file:
tests
-- bootstrap.php
-- test-sample.php
However, when I am in my plugin's directory and run phpunit I get No tests executed.
When I add the test file below the directory as:
<file>tests/test-sample.php</file>
and then run phpunit, I can see the test running.
Shouldn't it automatically detect the directory and files in the testsuite without the need of writing each test file one-by-one in the XML?
EDIT: phpunit -v output:
$ phpunit -v
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.14-1+ubuntu18.04.1+deb.sury.org+1
Configuration: /vagrant/Plugins/Framework/phpunit.xml.dist
Time: 1 second, Memory: 26.00MB
EDIT 2: bootstrap.php file contents:
<?php
/**
* PHPUnit bootstrap file
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require realpath(dirname(__FILE__) . '/../..') . '/myplugin/myplugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';