PHPUnit-在使用配置文件时“不执行测试”

问题

为了提高代码质量,我决定尝试学习如何使用单元测试来测试代码,而不是使用平庸的测试解决方案。

我决定使用个人库的作曲器来安装 PHPUnit,这样就可以实现通用的数据库功能。起初我没有 PHPUnit 的配置文件,当我运行如下命令时:

$ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest

请注意,这是一个终端命令,所以我没有包含 .php扩展。上面提到的 GeneralStringFunctionsTest 实际上是一个 GeneralStringFunctionsTest.php文件。

结果如我所料:

时间: 31毫秒,内存: 2.75兆

OK (1个测试,1个断言)

然后,我尝试使用配置文件自动加载测试套件,而不是每次都必须手动键入文件。我在根目录中创建了一个名为 phpunit.xml的文件,并在该文件中输入以下内容: http://pastebin.com/0j0L4WBD:

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

现在,当我运行命令时:

phpunit

我得到以下输出:

PHPUnit 4.5.0作者: Sebastian Bergmann 及其贡献者。

从/Users/muyiwa/Projects/DatabaseHelper/phPunit.xml 读取的配置

时间: 16毫秒,内存: 1.5兆

不执行测试!

如果有用的话,我的目录结构如下:
Src-顶级目录(包含所有源代码)
Test-顶级目录(所有测试的结构都与我的 Src文件夹相同)
作曲家第三方文件

我还有作曲家 json 和锁文件,以及顶层的 phPunit xml 文件作为文件。

我尝试过的事情

  • phpunit.xml中的目录更改为 tests/GeneralStringFunctions
  • phpunit.xml中的目录更改为 ./tests
  • phpunit.xml文件移动到 tests目录,然后将目录更改为 ./而不是 tests
  • 将后缀属性添加到 phpunit.xml中的目录标记中,以指定“ Test”作为显式后缀。
70702 次浏览

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.

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!

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)

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!

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 {...}

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

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()
{
...
}

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

You Need Just To Call It From Vendor File

vendor\bin\phpunit Notice \ Not /

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

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.

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". :)

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

if you are using PHPSTORM go to Settings then goto

  • Test Frameworks

    and click + and choose

  • PHPUnit Local then

  • then paste this like in path to script field

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

  • click OK

  • HAPPY TESTING

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.

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

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).

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.

Instead of run phpunit

use

vendor\bin\phpunit

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

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.

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

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

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

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

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

/**
* @group active
**/

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

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 ...;
}

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.