PHPUnit - 'No tests executed' when using configuration file

65,798

Solution 1

For what it's worth (being late), I ran into this recently while I was making a new Laravel 5.1 project for a simple website. I tried to debug it and was confused when I tried:

php artisan make:test homeTest

(which has a default test that just asserts true is true)

and saw the output

No tests executed!

What the problem ended up being for me was related to my PHP installation -- "phpunit" was globally registered and configured differently, whereas the phpunit that came with the Laravel installation was configured just right and ran perfectly.

So the fix is running the vendor's configured phpunit (from the same root directory as app/ and tests/):

./vendor/bin/phpunit

Hope that helps someone else!

Solution 2

Your XML file is fine as it is. However, you have to make sure that the PHP files in your tests/ folder are named as follows:

tests/Test.php <--- Note the uppercase "T"
tests/userTest.php
tests/fooBarTest.php
etc.

The filenames must end with "Test.php". This is what PHPUnit is looking for within directories.

Furthermore, every test method must either have a name that starts with "test" OR an @test annotation:

public function testFooBar()
{
    // Your test code
}

or:

 /**
  * @test
  */
 public function fooBarTest() {
     // test code here
 }

Hope that helps!

Solution 3

On windows use the following command on terminal

.\vendor\bin\phpunit

that's if the command

phpunit

returns "No tests executed!"

while on Mac

./vendor/bin/phpunit

Hope it helps.

Solution 4

I had the same problem after PHPUnit on our virtual machines updated to version 6. Even --debug and --verbose said nothing useful, just "No tests executed". In the end it turned out that classes and namespaces were changed in the new version and it just didn't want to execute the files that contained references to old classes. The fix for me was just to replace in every test case this:

class MyTestCase extends \PHPUnit_Framework_TestCase {...}

with:

use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase {...}

Solution 5

I realize this is super old, but it just happened to me too. Hopefully this will help someone.

My problem was that I forgot the '@' symbol in /** @test */

WRONG:

/** test */
function a_thread_can_be_deleted()
{
    ...
}

RIGHT:

/** @test */
function a_thread_can_be_deleted()
{
    ...
}
Share:
65,798

Related videos on Youtube

Muyiwa Olu
Author by

Muyiwa Olu

Updated on April 14, 2022

Comments

  • Muyiwa Olu
    Muyiwa Olu about 2 years

    The Problem

    To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions.

    I decided to install PHPUnit using composer for a personal library that allows me to achieve common database functions. At first I didn't have a configuration file for PHPUnit and when I ran commands like:

    $ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest
    

    Please note that this is a terminal command, so I didn't include the .php extension. The GeneralStringFunctionsTest referred to above is actually a GeneralStringFunctionsTest.php file.

    The output is what I expected:

    Time: 31 ms, Memory: 2.75Mb

    OK (1 test, 1 assertion)

    I then tried to use a configuration file to automatically load the test suite instead of having to manually type in the file every time. I created a file called phpunit.xml in my root directory, and entered the following into the file: http://pastebin.com/0j0L4WBD:

    <?xml version = "1.0" encoding="UTF-8" ?>
    <phpunit>
        <testsuites>
            <testsuite name="Tests">
                <directory>tests</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    Now, when I run the command:

    phpunit
    

    I get the following output:

    PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

    Configuration read from /Users/muyiwa/Projects/DatabaseHelper/phpunit.xml

    Time: 16 ms, Memory: 1.50Mb

    No tests executed!

    In case it's helpful, my directory structure is as follows:
    src - Top level directory (with all my source code)
    tests - Top level directory (with all my tests, structured the same as my src folder)
    vendor - Composer third party files

    I also have the composer json and lock file, as well as the phpunit xml file in the top level as files.

    Things I've Tried

    • Changing the directory in phpunit.xml to tests/GeneralStringFunctions
    • Changing the directory in phpunit.xml to ./tests
    • Moving the phpunit.xml file to the tests directory and then changing the directory to be ./ instead of tests.
    • Adding a suffix attribute to the directory tag in phpunit.xml to specify "Tests" as the explicit suffix.
    • hek2mgl
      hek2mgl about 9 years
      Is tests/GeneralStringFunctions/GeneralStringFunctionsTest a folder or a file name?
    • Admin
      Admin about 9 years
      @hek2mgl It's a filename, it's actually called GeneralStringFunctionsTest.php. In the command line interface, I didn't enter the .php extension because it worked without it.
    • hek2mgl
      hek2mgl about 9 years
      OK, then your configuration should work. Btw, if you specify a suffix it should be Test.php rather than Test in your case, but however, you are free to omit that since Test.php is the default value.
    • Admin
      Admin about 9 years
      @hek2mgl Thanks for the heads up! Do you have any idea why my test isn't running with the configuration file phpunit.xml?
    • ThW
      ThW about 9 years
      Why don't you show us the configuration file?
    • Admin
      Admin about 9 years
      @ThW Hi, I did - sorry if it wasn't too clear: pastebin.com/0j0L4WBD.
    • hakre
      hakre about 9 years
      Which operating system are you using? which PHP version?
    • Admin
      Admin about 9 years
      @hakre Hey, I am using PHP 5.6.2 and OS X Yosemite.
  • Admin
    Admin about 9 years
    Hi, sorry if I didn't make it clear in the question, but I did add a test suite and corresponding name to the XML file. A copy can be found here: pastebin.com/0j0L4WBD.
  • Aine
    Aine about 9 years
    Try <directory>./tests</directory>
  • Admin
    Admin about 9 years
    Thanks for the update, unfortunately I still get the same problem with ./tests as my directory. Time: 28 ms, Memory: 1.50Mb No tests executed!
  • Admin
    Admin about 9 years
    Unfortunately the same problem :( Configuration read from /Users/muyiwa/Projects/Web Development/DatabaseHelper/phpunit.xml Time: 66 ms, Memory: 1.50Mb No tests executed!. It definitely works when I manually reference it, it just doesn't like loading from the configuration file for some reason :(
  • Aine
    Aine about 9 years
    Last guess. Try moving the phpunit.xml file into the tests/ directory. In the file, change the directory to <directory>./</directory>.
  • Admin
    Admin about 9 years
    Still the same output, I'm afraid. I can't get my head round it. Works perfectly when I manually call the file in a command argument, i.e. phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest.
  • Steven Scott
    Steven Scott about 9 years
    Try using a complete path in the phpunit.xml file to your test directory. I believe the PHPUnit batch file on Windows changes the base directory you are running in, so your relative path ('./tests') may not be correct. If you are not on Windows, the relative path could still be the issue.
  • hakre
    hakre about 9 years
    @Aine: There is no need to prefix the (already relative) path to the directory with "./". Just FYI.
  • Admin
    Admin about 9 years
    @StevenScott Something slightly different is happening now. I updated my .bash_profile to use my MAMP's PHP version, and I also made sure to update the php.ini file to remove short tags. Now when I run it, there are no tests, but it echoes out the content of the xml file like and then says no tests executed.
  • Admin
    Admin about 9 years
    <?xml version="1.0" encoding="UTF-8" ?> <phpunit> <testsuites> <testsuite name="Tests"> <directory>/Users/muyiwa/Projects/webdev/DatabaseHelper/phpu‌​nit.xml</directory> </testsuite> </testsuites> </phpunit>PHPUnit 4.5.0 by Sebastian Bergmann and contributors. Configuration read from /Users/muyiwa/Projects/webdev/DatabaseHelper/phpunit.xml Time: 77 ms, Memory: 1.50Mb No tests executed!
  • checksum
    checksum almost 8 years
    or you can use --test-suffix=anything.php to override
  • Admin
    Admin about 7 years
    I think this is most likely the correct answer. I've since removed the project so I have no way to double check, but for future projects with composer, I've always used vendor/bin/phpunit instead of the global phpunit binary. I've even gone as far as removing the global phpunit binary so I don't accidentally make the same mistake again -- and it hasn't popped up since. I shall mark this as the accepted answer, as it most likely is the reason.
  • ira
    ira almost 7 years
    calling ./vendor/bin/phpunit did the trick for me on a fresh laravel 5.4 install
  • Tama
    Tama almost 7 years
    Ah, yes, while it doesn't look like this was the answer to the original question it solved my issue after upgrading to 6.x!
  • sqsinger
    sqsinger almost 7 years
    Similarly, this helped me while working on a Wordpress project. The same problem: "phpunit" can be globally registered and configured differently. Thank you for sharing!
  • Timur Samkharadze
    Timur Samkharadze over 6 years
    for me it was useful to replace relative path to test folder with absolute path.
  • Noitidart
    Noitidart over 6 years
    This fixed it for me, but is there a way to globally fix the phpunit? It would be nice if i can just type phpunit
  • Ganesh K
    Ganesh K over 6 years
    @Noitidart you can create an alias for ./vendor/bin/phpunit
  • kiwicomb123
    kiwicomb123 over 6 years
    Is there a way to configure the global phpunit so that I can just call phpunit? (assuming I am not using composter and do not have the vender dir)
  • Wesley Brian Lachenal
    Wesley Brian Lachenal about 6 years
    That alias thing works but when you have to use [--filter] or some sort. It just calls everything and it ignores that.
  • RoboBear
    RoboBear about 6 years
    @kiwicomb123 - One idea is you could try updating your alias or command for running PHPUNIT and specfiy the config file in that command, such as "phpunit --configuration /path/to/config.xml". As for the "--filter" issue -- which I think is to specify one test or part of a test to run? -- you could try reading phpunit.de/manual/current/en/appendixes.configuration.html for help on using <filter>...</filter> blocks instead of on the command line. That way when you run via specifying the config file it should also parse the correct filter rules.
  • Goke Obasa
    Goke Obasa about 6 years
    Thanks so much for your answer, this really helps.
  • WindSaber
    WindSaber over 5 years
    Great, I was able to run test after that
  • Jonathan
    Jonathan about 5 years
    How do I override this in the configuration file?
  • aibarra
    aibarra over 4 years
    over 2 years later, and this was it for me. it really has to be exactly that format for the comment. i had a missing space in there. it didn't detect it. what's a little annoying, in laravel 6.1 i used the artisan make command and it doesn't include that comment
  • giovannipds
    giovannipds over 4 years
    Be sure there's no phpunit at your global path (you can check it running something like phpunit --version and see if it's matching to what're you expecting. Xampp usually installs a phpunit at the same php bin dir. That was the problem to me.
  • giovannipds
    giovannipds over 4 years
    On windows you can check if there's no phpunit already running globally. Xampp usually puts a phpunit at the same php bin dir and that can be a problem. You can check phpunit version running phpunit --version.
  • Liga
    Liga over 4 years
    using vendor\bin\phpunit (on Windows) worked for me!
  • Sundar
    Sundar almost 4 years
    This answer is useful
  • DAMIEN JIANG
    DAMIEN JIANG over 3 years
    I run into the problem when I was developing a package. When I run 'php artisan make:test SomeTest' and moved it to the package tests directory, the basic testExample method's comment only contains a basic description and @return void and doesn't contain '@test', which is the reason phpunit couldn't identify it as a test.
  • confirmator
    confirmator over 3 years
    This was the issue for me when upgrading from PHPUnit 5.7 to 8.5 as well - thank you for the tip!
  • JRod
    JRod over 3 years
    I was trying to get it working with phpunit defined global and finally work in this way, I'm using MacOS and the command line is phpunit --debug ./
  • Mycodingproject
    Mycodingproject over 3 years
    I'm one of the most ignorant person in the world when it comes to unit testing so it would be nice the reason for this but it really helped. Thanks anyway!
  • Babak Asadzadeh
    Babak Asadzadeh about 3 years
    you saved my day
  • Admin
    Admin about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • clockw0rk
    clockw0rk about 2 years
    why remembering suffix magic when you can just name the file AcceptanceXYZTest.php - this way everybody will instantly know that it's a test. You can ofc. be super unique and call it XYZTestCaseFile.php and use a suffix - but where's the net gain here?
  • Alkari
    Alkari almost 2 years
    Adding test to the methods fixed it, thank you