111

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.
thanksd
  • 54,176
  • 22
  • 157
  • 150
  • Is `tests/GeneralStringFunctions/GeneralStringFunctionsTest` a folder or a file name? – hek2mgl Mar 27 '15 at 11:21
  • @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. –  Mar 27 '15 at 11:32
  • 1
    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. – hek2mgl Mar 27 '15 at 11:35
  • @hek2mgl Thanks for the heads up! Do you have any idea why my test isn't running with the configuration file `phpunit.xml`? –  Mar 27 '15 at 11:40
  • Why don't you show us the configuration file? – ThW Mar 27 '15 at 13:41
  • @ThW Hi, I did - sorry if it wasn't too clear: http://pastebin.com/0j0L4WBD. –  Mar 27 '15 at 13:42
  • Which operating system are you using? which PHP version? – hakre Mar 27 '15 at 19:56
  • @hakre Hey, I am using PHP 5.6.2 and OS X Yosemite. –  Mar 27 '15 at 22:04

30 Answers30

207

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
starball
  • 20,030
  • 7
  • 43
  • 238
RoboBear
  • 5,434
  • 2
  • 33
  • 40
  • 4
    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. –  Feb 17 '17 at 11:32
  • 4
    calling `./vendor/bin/phpunit` did the trick for me on a fresh laravel 5.4 install – ira Apr 28 '17 at 14:18
  • 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! – sqsinger Jul 16 '17 at 07:10
  • 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` – Noitidart Oct 15 '17 at 21:33
  • 2
    @Noitidart you can create an `alias` for `./vendor/bin/phpunit` – Ganesh K Oct 23 '17 at 04:15
  • 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) – kiwicomb123 Dec 21 '17 at 12:29
  • That alias thing works but when you have to use [--filter] or some sort. It just calls everything and it ignores that. – Wesley Brian Lachenal Mar 04 '18 at 00:18
  • @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 https://phpunit.de/manual/current/en/appendixes.configuration.html for help on using ... 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. – RoboBear Mar 09 '18 at 21:20
  • 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`. – giovannipds Dec 11 '19 at 18:23
  • 1
    using ````vendor\bin\phpunit```` (on Windows) worked for me! – Liga Jan 06 '20 at 06:17
129

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!

tommytucker7182
  • 213
  • 1
  • 5
  • 11
Lionel
  • 1,949
  • 3
  • 17
  • 27
23

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.

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
Ndong Akwo
  • 592
  • 5
  • 13
  • Thanks so much for your answer, this really helps. – Goke Obasa Apr 18 '18 at 01:03
  • 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 Dec 11 '19 at 18:22
  • 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! – Mycodingproject Jan 01 '21 at 12:33
16

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 {...}
igors
  • 358
  • 2
  • 5
  • 1
    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! – Tama Jun 05 '17 at 13:31
  • This was the issue for me when upgrading from PHPUnit 5.7 to 8.5 as well - thank you for the tip! – confirmator Sep 02 '20 at 21:26
11

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()
{
    ...
}
Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
  • 1
    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 – aibarra Oct 10 '19 at 15:14
  • 1
    This answer is useful – Sundar May 10 '20 at 11:45
  • 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. – DAMIEN JIANG Aug 21 '20 at 00:20
8

I pulled my hair for 10 minutes before i decided to use --debug (good way to go by the way) to discover the simple fact that file name didn't respect the naming convention, i had an extra "s" at the end.

wrong

CreateAdminTests

right

CreateAdminTest

hope this note could help for someone

chebaby
  • 7,362
  • 50
  • 46
8

Came late to the party, but this info may help others.

Above solutions did not work for me. As of Laravel 7.x, by default, PHPUnit executes only PHP files which are suffixed with "Test.php". For example if you create a test, naming it CreateUser will not work, whereas naming it CreateUserTest will work.

To overcome the limitation, go to phpunit.xml and modify suffix attribute from directory elements:

...
<testsuite name="Unit">

    <!-- modify suffix -->
    <directory suffix=".php">./tests/Unit</directory>

</testsuite>
<testsuite name="Feature">

    <!-- modify suffix -->
    <directory suffix=".php">./tests/Feature</directory>

</testsuite>
...

This will instruct PHPUnit to run all files with .php extension from directories. Note to clear app's cache after updating phpunit.xml:

php artisan config:cache

Additional information about PHPUnit XML configuration can be found here: https://phpunit.de/manual/6.5/en/appendixes.configuration.html

user8555937
  • 2,161
  • 1
  • 14
  • 39
6

You Need Just To Call It From Vendor File

vendor\bin\phpunit Notice \ Not /

Zymawy
  • 1,511
  • 16
  • 15
5

Instead of run phpunit

use

vendor\bin\phpunit

Arun Ganesan
  • 61
  • 1
  • 2
2

if you are using PHPSTORM go to Settings then goto

  • Test Frameworks

    and click + and choose

  • PHPUnit Local then

  • Use Composer Auto Loader then paste this like in path to script field

  • C:\{YOUR PROJECT NAME}\vendor\autoload.php

  • click OK

  • HAPPY TESTING

Community
  • 1
  • 1
Tahseen Alaa
  • 342
  • 4
  • 9
2

Check phpunit.xml file, look inside testsuites.

My version of phpunit (2019) is looking for files ending (suffix) *Test.php . So, make sure all the test files are named properly (ex.: BookTest.php is correct, BookTests.php is not, BookTestCase.php is not).

Jan Polzer
  • 361
  • 2
  • 5
2

The function names in the test file must either be prefixed with test or there should be a comment added before function

/** @test */

Make sure that it is not

/* @test */

because that doesn't work. there needs to be two asterisks after slash not one.

Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
2

I had the same issue and I found all my configs and syntaxs were OK. I don't know why but simply adding the word "tests" (in plural), which is the name inside the App folder containing all tests, worked to me pretty well:

[terminal] vendor/bin/phpunit tests

If my alias works with plain "phpunit" I would do the same

[terminal] phpunit tests

Same with filters

[terminal]  vendor/bin/phpunit --filter 'Tests\\Routes\\AnyTest' Tests

Notice that "tests" at the end is in plural as is the same folder name inside App folder.

Hope it help and save time to someone else.

Best regards

alex t
  • 851
  • 8
  • 9
2

if you don't have Test Class

        php artisan make:test TestName //default is future testing
        php artisan make:test TestName --unit //for unit testing 

then you will find the test file in your test folder under subfolder unit or future if you want to execute all the test files use

note: for testing, you can edit your phpuint.xml file the best approach is if you use sqlite to set that up got to tag then after the last line you add this two lines

   <server name="DB_CONNECTION" value="sqlite"/>
   <server name="DB_DATABASE" value=":memory:"/>

you can execute your test file using vendor/bin/PHPUnit //this will execute every test file vendor/bin/PHPUnit if you wish to execute a single function you can use the

         vendor/bin/PHPUnit --filter function_name or classname

and lastly, use /** @test*/ in every test in order for you PHPUnit to know this is a test

1

Have you added a test suite to you phpunit.xml file?

<phpunit>
    <testsuite name="app1" >
        <directory>./</directory>
    </testsuite>
</phpunit>

You can add multiple directories in there.

Aine
  • 2,638
  • 3
  • 30
  • 40
  • 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: http://pastebin.com/0j0L4WBD. –  Mar 27 '15 at 12:00
  • Try ./tests – Aine Mar 27 '15 at 12:02
  • 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!` –  Mar 27 '15 at 12:06
  • 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 :( –  Mar 27 '15 at 12:14
  • Last guess. Try moving the phpunit.xml file into the tests/ directory. In the file, change the directory to ./. – Aine Mar 27 '15 at 12:21
  • 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`. –  Mar 27 '15 at 12:26
  • 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. – Steven Scott Mar 27 '15 at 15:32
  • @Aine: There is no need to prefix the (already relative) path to the directory with "`./`". Just FYI. – hakre Mar 27 '15 at 20:00
  • @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. –  Mar 27 '15 at 22:24
  • ` /Users/muyiwa/Projects/webdev/DatabaseHelper/phpunit.xml 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!` –  Mar 27 '15 at 22:24
1

the class and file name has no Test in them e.g "AuthorizationTest"

flams
  • 11
  • 4
  • 1
    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](/help/how-to-answer). – Community Mar 09 '22 at 14:10
0

For me, using phpunit --debug showed me which test it was not executing, inside, I had

$this->visit('/')
         ->see('Laravel');

and I think because the directory was protected with .htaccess authentication, it could not get through to visit the page

The solution for me was to take out this test (or most likely take out .htaccess authentication)

relipse
  • 1,730
  • 1
  • 18
  • 24
0

Mine was a bit funny.

When I used php artisan make:test I accidentally put .php like ProductRewardPointController.php which created ProductRewardPointController.php.php and phpunit simply ignored it.

I just delete the extra .php and things back to normal

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Apit John Ismail
  • 2,047
  • 20
  • 19
0

This is very late but I hope it helps someone.

I got my tests to run by using an absolute reference. folder structure [ project/tests/test.php]

my directory line looked like this ./tests/test.php

Codebender
  • 195
  • 1
  • 10
0

A little bit on the side maybe, but if you are (like me) using Laravel in Vagrant, make sure you are running phpunit inside of the vagrant box and not on the "windows side". :)

martinethyl
  • 184
  • 1
  • 12
0

I had the issue of no tests being executed, even when things were set up fine.

The cause was the namespace was not the first command of the file, it was after some doc-block comments.

reverting caused phpunit to see the tests and run correctly.

pgee70
  • 3,707
  • 4
  • 35
  • 41
0

I had the same issue of No tests executed!, solved by keeping the same name of file and class name.

Asif
  • 350
  • 2
  • 10
0

If you are using IDEs like JetBrains PHPStorm, please also notice that: in the Run/Debug Configurations window, the Test scope needs to be set to directory and point that directory to where your tests folder located.

It just took me half an hour to figure out I forgot to set the directory. You can use global phpunit.phar as long as you set test scope and the directory correctly, the IDE will handle other stuff for you.

Zhwt
  • 426
  • 3
  • 13
0

If you're using @dataprovider make sure the visibility of its method is public that was my problem

Mwthreex
  • 913
  • 1
  • 11
  • 24
0

For me with composer it works with both ./vendor/bin/phpunit and phpunit, I dont have phpunit installed globally.

the answer from @alext helped me to solved the issue.

So if you check the phpunit manual by running phpunit -h output:

PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

Usage: phpunit [options] UnitTest [UnitTest.php]
       phpunit [options] <directory>

it requires a directory or file. The [options] are optional, you can pass them using phpunit.xml file

Because your test is in tests, So you should try with phpunit tests If it doesnt work, let try with the sample from phpunit here: https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html

vanduc1102
  • 5,769
  • 1
  • 46
  • 43
0

In my case I added annotation @group active to my class

/**
* @group active
**/
clockw0rk
  • 576
  • 5
  • 26
Andrii Sukhoi
  • 749
  • 5
  • 14
0

There are many possible answers to this question. In case someone gets into same situation as I did - the cause for me getting the same message as the OP, was that my data-provider function was private.

I changed it to public and it all started working.

I speak about the data-provider as in here:

/**
 * @dataProvider myProvider
 * @return void
 */
public function testGetWhatever()
{
    $this->assertTrue(true);
}

public function myProvider()
{
    return ...;
}
userfuser
  • 1,350
  • 1
  • 18
  • 32
0

I got very similar problem that was caused by relative path to tests folder fixed by providing absolute path of the folder tests containing tests in phpunit.xml configuration file in testsuites section .

Overview:

My configuration is:

  • global installation of phpunit in /usr/local/bin/phpunit
  • folder with the test scripts somewhere in the filesystem in tests folder
  • tests written in filenameTest.php files
  • test classes named same as filename, ie class filenameTest extends TestCase {...}
  • test methods named public function test_descriptive_name() {...}

Running tests one by one using $ phpunit filenameTest.php all worked.
When I run the coverage report $ phpunit --coverage-html coverage I got the warning No tests executed!

$ phpunit --coverage-html coverage
PHPUnit 8.5.26 #StandWithUkraine

No tests executed!

Generating code coverage report in HTML format ... done [477 ms]

However it generated the coverage folder with output and list of test files with 0.00% Lines Functions and Methods Classes and Traits info!

From this point of view the phpunit knows what folder to be executed but nothing was.

I red this StackOverflow page and many others including the documentation about the phpunit.xml configuration file but still no success until I changed the path.

Solution:

In my case to make the coverage tests working I had to change the directory of the testsuite to absolute path.
So I replaced the <directory>tests</directory> with <directory>/absolute/path/to/the/folder/with/tests</directory> as follows:

<?xml version = "1.0" encoding="UTF-8" ?>
<phpunit>
    <testsuites>
        <testsuite name="Name of the test Suite">
            <directory>/absolute/path/to/the/folder/with/tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

Now everything works as expected. Hope this info will be useful to someone.

ino
  • 2,345
  • 1
  • 15
  • 27
  • I have the same case like u now, phpunit global installation, how u are managing the absolute path for different env(local, test, production), please? – Thamer Oct 19 '22 at 15:09
  • 1
    @Thamer luckily both production and development environments are both on Linux so I use symbolic links to simulate the production paths ;) – ino Oct 20 '22 at 06:33
0

I got the same error, if I tried to run specially one testmethod in my testclass. I did not got any exception- or error-message. I got only in PHPStorm:

PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

No tests executed!

Process finished with exit code 0

All the other testmethods in the same class worked fine, if I separately called them in PHPStorm!

The reason was, that php can't convert a DateTime-object into a string.

I had a loop, which build systematically my testparameters in my dataprovider. The array of parameters contained a dynamically generated test-message. After changing the dynamically message from

'message' => 'The estimates date of sunposition `' . $myParams['pos'] . '` is defined at  the time `' .
    $stopValue . '` for the next period: ' . print_r($myParams['nextActive'], true) . '.',

to

'message' => 'The estimates date of sunposition `' . $myParams['pos'] . '` is defined at  the time `' .
    $stopValue->format('Y-m-d H:i:s') . '` for the next period: ' . print_r($myParams['nextActive'], true) . '.',

everything worked fine.

-2

using de cmd console resolved this problem passing the enterely path Test realized

I did't find another way to do it It does not work from this way

I hope this was helpful for someone

mcrsftDev
  • 11
  • 2
  • 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 ./` – JRod Dec 16 '20 at 23:44