PHPUnit - 使用配置文件时'没有执行测试'

时间:2022-10-15 19:41:33

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:

我决定使用composer安装PHPUnit用于个人库,这使我可以实现常见的数据库功能。起初我没有PHPUnit的配置文件,当我运行如下命令:

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

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

The output is what I expected:

输出是我的预期:

Time: 31 ms, Memory: 2.75Mb

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

OK (1 test, 1 assertion)

好的(1个测试,1个断言)

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:

然后,我尝试使用配置文件自动加载测试套件,而不必每次都手动输入文件。我在我的根目录中创建了一个名为phpunit.xml的文件,并在文件中输入以下内容: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.

Sebastian Bergmann和贡献者的PHPUnit 4.5.0。

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

配置从/Users/muyiwa/Projects/DatabaseHelper/phpunit.xml中读取

Time: 16 ms, Memory: 1.50Mb

时间:16毫秒,内存: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

如果它有用,我的目录结构如下:src - *目录(包含我的所有源代码)测试 - *目录(包含我的所有测试,结构与我的src文件夹相同)供应商 - Composer第三方文件

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

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

Things I've Tried

  • Changing the directory in phpunit.xml to tests/GeneralStringFunctions
  • 将phpunit.xml中的目录更改为tests / GeneralStringFunctions
  • Changing the directory in phpunit.xml to ./tests
  • 将phpunit.xml中的目录更改为./tests
  • Moving the phpunit.xml file to the tests directory and then changing the directory to be ./ instead of tests.
  • 将phpunit.xml文件移动到tests目录,然后将目录更改为./而不是test。
  • Adding a suffix attribute to the directory tag in phpunit.xml to specify "Tests" as the explicit suffix.
  • 将后缀属性添加到phpunit.xml中的目录标记,以将“测试”指定为显式后缀。

12 个解决方案

#1


96  

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:

为了它的价值(迟到),我最近遇到了这个问题,而我正在为一个简单的网站制作一个新的Laravel 5.1项目。我尝试调试它,当我尝试时感到困惑:

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.

问题最终对我而言与我的PHP安装有关 - “phpunit”是全局注册和配置不同,而Laravel安装附带的phpunit配置恰到好处并且运行完美。

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

所以修复程序正在运行供应商配置的phpunit(来自与app /和tests /相同的根目录):

./vendor/bin/phpunit

Hope that helps someone else!

希望能帮助别人!

#2


58  

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:

您的XML文件很好。但是,您必须确保test /文件夹中的PHP文件的名称如下:

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

tests / Test.php <---注意大写的“T”测试/ userTest.php测试/ fooBarTest.php等。

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

文件名必须以“Test.php”结尾。这就是PHPUnit在目录中寻找的东西。

Furthermore, every test method must have a name that starts with "test":

此外,每个测试方法必须具有以“test”开头的名称:

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

Hope that helps!

希望有所帮助!

#3


9  

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:

我的虚拟机上的PHPUnit更新到版本6后,我遇到了同样的问题。甚至--debug和--verbose没有任何用处,只是“没有执行测试”。最后,事实证明在新版本中更改了类和命名空间,它只是不想执行包含对旧类的引用的文件。我的修复只是在每个测试用例中替换:

class MyTestCase extends \PHPUnit_Framework_TestCase {...}

with:

有:

use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase {...}

#4


2  

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 */

我的问题是我忘了/ ** @test * /中的'@'符号

WRONG:

错误:

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

RIGHT:

对:

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

#5


2  

On windows use the following command on terminal

在Windows上,在终端上使用以下命令

.\vendor\bin\phpunit

that's if the command

那是命令

phpunit

returns "No tests executed!"

返回“没有执行测试!”

while on Mac

在Mac上

./vendor/bin/phpunit

Hope it helps.

希望能帮助到你。

#6


1  

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

你有没有为你的phpunit.xml文件添加一个测试套件?

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

You can add multiple directories in there.

您可以在其中添加多个目录。

#7


0  

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

对我来说,使用phpunit --debug告诉我它没有执行哪个测试,里面,我有

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

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

我认为因为该目录受到.htaccess身份验证的保护,所以无法访问该页面

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

我的解决方案是取出这个测试(或者最有可能取出.htaccess身份验证)

#8


0  

Mine was a bit funny.

我有点好笑。

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

当我使用php artisan make:test我意外地把.php像ProductRewardPointController.php一样创建了ProductRewardPointController.php.php而phpunit却忽略了它。

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

我只是删除额外的.php和事情恢复正常

#9


0  

You Need Just To Call It From Vendor File

你只需要从供应商文件中调用它

vendor\bin\phpunit Notice \ Not /

vendor \ bin \ phpunit注意\不是/

#10


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]

我通过使用绝对引用来运行我的测试。文件夹结构[project / tests / test.php]

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

我的目录行看起来像这样./tests/test.php

#11


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

也许有一点点,但如果你(像我一样)在Vagrant中使用Laravel,请确保你在流浪盒内运行phpunit而不是在“windows side”。 :)

#12


-2  

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

使用de cmd控制台解决了这个问题,传递了enterely路径测试实现

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

我没有找到另一种方法来做它从这种方式不起作用

I hope this was helpful for someone

我希望这对某人有帮助

#1


96  

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:

为了它的价值(迟到),我最近遇到了这个问题,而我正在为一个简单的网站制作一个新的Laravel 5.1项目。我尝试调试它,当我尝试时感到困惑:

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.

问题最终对我而言与我的PHP安装有关 - “phpunit”是全局注册和配置不同,而Laravel安装附带的phpunit配置恰到好处并且运行完美。

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

所以修复程序正在运行供应商配置的phpunit(来自与app /和tests /相同的根目录):

./vendor/bin/phpunit

Hope that helps someone else!

希望能帮助别人!

#2


58  

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:

您的XML文件很好。但是,您必须确保test /文件夹中的PHP文件的名称如下:

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

tests / Test.php <---注意大写的“T”测试/ userTest.php测试/ fooBarTest.php等。

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

文件名必须以“Test.php”结尾。这就是PHPUnit在目录中寻找的东西。

Furthermore, every test method must have a name that starts with "test":

此外,每个测试方法必须具有以“test”开头的名称:

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

Hope that helps!

希望有所帮助!

#3


9  

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:

我的虚拟机上的PHPUnit更新到版本6后,我遇到了同样的问题。甚至--debug和--verbose没有任何用处,只是“没有执行测试”。最后,事实证明在新版本中更改了类和命名空间,它只是不想执行包含对旧类的引用的文件。我的修复只是在每个测试用例中替换:

class MyTestCase extends \PHPUnit_Framework_TestCase {...}

with:

有:

use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase {...}

#4


2  

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 */

我的问题是我忘了/ ** @test * /中的'@'符号

WRONG:

错误:

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

RIGHT:

对:

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

#5


2  

On windows use the following command on terminal

在Windows上,在终端上使用以下命令

.\vendor\bin\phpunit

that's if the command

那是命令

phpunit

returns "No tests executed!"

返回“没有执行测试!”

while on Mac

在Mac上

./vendor/bin/phpunit

Hope it helps.

希望能帮助到你。

#6


1  

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

你有没有为你的phpunit.xml文件添加一个测试套件?

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

You can add multiple directories in there.

您可以在其中添加多个目录。

#7


0  

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

对我来说,使用phpunit --debug告诉我它没有执行哪个测试,里面,我有

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

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

我认为因为该目录受到.htaccess身份验证的保护,所以无法访问该页面

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

我的解决方案是取出这个测试(或者最有可能取出.htaccess身份验证)

#8


0  

Mine was a bit funny.

我有点好笑。

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

当我使用php artisan make:test我意外地把.php像ProductRewardPointController.php一样创建了ProductRewardPointController.php.php而phpunit却忽略了它。

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

我只是删除额外的.php和事情恢复正常

#9


0  

You Need Just To Call It From Vendor File

你只需要从供应商文件中调用它

vendor\bin\phpunit Notice \ Not /

vendor \ bin \ phpunit注意\不是/

#10


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]

我通过使用绝对引用来运行我的测试。文件夹结构[project / tests / test.php]

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

我的目录行看起来像这样./tests/test.php

#11


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

也许有一点点,但如果你(像我一样)在Vagrant中使用Laravel,请确保你在流浪盒内运行phpunit而不是在“windows side”。 :)

#12


-2  

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

使用de cmd控制台解决了这个问题,传递了enterely路径测试实现

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

我没有找到另一种方法来做它从这种方式不起作用

I hope this was helpful for someone

我希望这对某人有帮助