PHPUnit - 'No tests executed' when using configuration file
If you are transitioning from manual test execution to a configured suite, you might encounter a scenario where your phpunit.xml file is read successfully, yet the CLI reports "No tests executed!". This happens when the directory path defined in your configuration does not match how PHPUnit resolves file paths relative to that configuration file.
In the scenario described, the user runs PHPUnit from the project root (where phpunit.xml, composer.json, and src reside). The configuration file contains:
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
The error message "No tests executed!" indicates that PHPUnit scanned the tests directory relative to the location of phpunit.xml, found no files matching the naming convention, or failed to locate the specific test class because the path resolution logic was flawed.
Relative Path Resolution and Directory Structure
PHPUnit determines the base path for scanning directories based on the location of the phpunit.xml file. If your configuration file is in the root directory (/Users/muyiwa/Projects/DatabaseHelper/phpunit.xml), the <directory>tests</directory> tag resolves to /Users/muyiwa/Projects/DatabaseHelper/tests.
The user's directory structure lists tests as a top-level directory alongside src and vendor. Therefore, the path should be correct. However, if PHPUnit cannot find the tests, it often means the files inside that directory do not match the default discovery rules or the file naming convention is incorrect.
The File Naming Convention Trap
A common pitfall in PHPUnit 4.x (and subsequent versions) is the strict requirement for test class names to end with Test. The source material mentions a file named GeneralStringFunctionsTest.php. If this file contains a class definition, that class name must strictly follow the pattern {ClassName}Test.
If the file inside tests is named GeneralStringFunctionsTest.php, PHPUnit expects a class defined within it to be named exactly GeneralStringFunctionsTest. If the class was named GeneralStringFunctions or if the file path resolution caused PHPUnit to look for a different file entirely, the discovery step would yield zero classes.
Additionally, ensure that the files inside the tests directory have execute permissions and are not being ignored by .gitignore or other exclusion filters, though this is less likely to cause a "No tests executed" error than path mismatches.
Fixing the Configuration
To resolve the issue without moving files or changing names, verify that the phpunit.xml file is indeed located in the directory you intend to run from. If you move phpunit.xml into the tests folder, you must update the <directory> tag accordingly.
For example, if you move the config file down one level:
# Move phpunit.xml into tests/
mv phpunit.xml tests/phpunit.xml
You would then need to run PHPUnit from within that directory or adjust your command:
phpunit --configuration tests/phpunit.xml
Inside this new tests/phpunit.xml, the configuration would look like this:
<phpunit>
<testsuites>
<testsuite name="Tests">
<!-- Now relative to the phpunit.xml file in tests/ -->
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
This approach ensures that . resolves to the current directory (which is now tests), and PHPUnit scans the immediate children for classes ending in Test.
Alternative: Explicit File Listing
If you prefer not to rely on directory discovery or if the file structure becomes complex, you can explicitly list the files in your configuration. This removes any ambiguity about path resolution:
<phpunit>
<testsuites>
<testsuite name="Tests">
<file>tests/GeneralStringFunctions/GeneralStringFunctionsTest.php</file>
<!-- Add other specific files here -->
</testsuite>
</testsuites>
</phpunit>
This method guarantees that PHPUnit looks exactly at the specified file path, regardless of how many subdirectories exist within tests. While less scalable for large suites, it is an effective debugging step when discovery fails.
Conclusion on Path Resolution
The "No tests executed" error in this context is almost always a result of the relative path between the configuration file and the test directory not aligning with PHPUnit's internal resolution logic. By ensuring the phpunit.xml file is in the expected location or explicitly adjusting the <directory> tag to reflect the new base path, you restore the automatic suite loading capability.