PHPUnit does not automatically detect files in directory

Viewed 975

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';
1 Answers
  • phpunit without command-line options

You should wonder what is your current working directory when you run phpunit without command-line options as you can see in the phpunit 6.5 doc. If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.

  • phpunit with command-line options

If you want to run phpunit from a directory that does not content phpunit.xml (for example your plugin's directory), you'll have to pass the correct path :

phpunit --configuration /vagrant/Plugins/Framework/phpunit.xml.dist

As you configured bootstrap="tests/bootstrap.php" in your phpunit.xml.dist, phpunit will look for a bootstrap.php file in /vagrant/Plugins/Framework/tests directory.

Your bootstrap.php file contains these lines :

$_tests_dir = getenv( 'WP_TESTS_DIR' ); if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';

I suggest you replace them by : $_tests_dir = '/tmp/wordpress-tests-lib';

You could also add this in your ~/.bash_profile to avoid repeating the replacement :

export WP_TESTS_DIR='/tmp/wordpress-tests-lib'

Related