PHPUnit - 在测试套件下的目录标记中忽略后缀参数
PHPUnit - suffix argument ignored in directory tag under testsuite
我最近意识到,除非使用“php artisan 测试”,否则我无法一次 运行 所有 php 单元测试,我不明白为什么。我会使用其他选项,但我无法覆盖它。
我认为这与我的配置文件因语法更改而错误或我的目录设置错误或其他原因有关,因为我找不到其他任何东西。
套件找到了,但没有应用后缀,不管我写成“.php”还是“Tests.php”还是别的什么。我知道,因为如果我在最后用 Test 而不是 Tests 重命名我所有的测试文件,那么它们会突然被 phpunit 识别。我还尝试了所有目录选项,例如“./tests/Unit”和“./tests/Unit/*”,但无济于事。
如果有人知道发生了什么,我将不胜感激,因为这让我发疯。我花了一天时间才弄清楚我需要重命名我的文件才能使其正常工作,在这一点上我什至不想这样做,除非我绝对必须这样做,因为有这个后缀功能。
我只是想了解为什么它不起作用,以及如何正确使用它,希望能够学习如何正确使用phpunit。
我可以看到两个潜在的原因:
1) 您的phpunit.xml
可能配置不正确。
尝试更改“testsuite”和“coverage”部分,如下所示:
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
2) 确保所有测试方法都在方法的 DocBlock 中使用 @test
注释:
/** @test */
public function your_test_method_name()
{
...
}
参见:https://phpunit.readthedocs.io/en/9.5/annotations.html
As an alternative to prefixing your test method names with test, you can use the @test annotation in a method’s DocBlock to mark it as a test method.
如果您不包含文档块,请确保您的测试方法名称以“test”开头:
public function test_method_name()
{
...
}
参见:https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html
The tests are public methods that are named test*.
无法修复后缀。我尝试重新安装 xdebug、php 和 phpstorm。正如我所说,我最终只是重命名了文件。我能找到的唯一有效解决方案。
I know, because if I rename all my test files with Test at the end instead of Tests, then they suddenly get recognized by phpunit.
这听起来像是 phpunit 的默认配置,可能表明您试图用来控制行为的配置文件 无效 。也就是说,无论你如何 运行 Phpunit,它都没有配置文件和路径,例如:
/path/to/phpunit -- /path/to/test/directory
然后 Phpunit 将使用默认配置 - 但它必须在工作目录中找到它。因此,要么配置文件的名称与 phpunit 用于自动检测的名称不同,要么它们不在工作目录中,phpunit 将在不加载配置文件的情况下启动,只执行它在具有默认后缀的目录中可以找到的内容“Test.php”。
(在 Phpstorm 中,工作目录在调用 Phpunit 时可能会有所不同,即使在同一操作的 运行 之间,但这只是一个注释。)
你可以在Phpstorm中做什么:配置Phpunit配置文件来加载。一个好的方法是在 Settings -> PHP -> 中指定默认的 Phpunit 配置文件测试运行器部分中的测试框架 默认配置文件。
另一件事是在您已经创建的 运行 操作中明确指定工作目录,但它不会执行您想要的操作,或者通过修改 Phpunit 运行 操作模板。这是模板的屏幕截图:
检查有效性的一种方法是在配置中使用bootstrap文件并使其爆炸。然后你就知道有没有效果了。
此外,如果正在使用,PHpunit 通常会写出配置文件的名称。
我最近意识到,除非使用“php artisan 测试”,否则我无法一次 运行 所有 php 单元测试,我不明白为什么。我会使用其他选项,但我无法覆盖它。
我认为这与我的配置文件因语法更改而错误或我的目录设置错误或其他原因有关,因为我找不到其他任何东西。
套件找到了,但没有应用后缀,不管我写成“.php”还是“Tests.php”还是别的什么。我知道,因为如果我在最后用 Test 而不是 Tests 重命名我所有的测试文件,那么它们会突然被 phpunit 识别。我还尝试了所有目录选项,例如“./tests/Unit”和“./tests/Unit/*”,但无济于事。
如果有人知道发生了什么,我将不胜感激,因为这让我发疯。我花了一天时间才弄清楚我需要重命名我的文件才能使其正常工作,在这一点上我什至不想这样做,除非我绝对必须这样做,因为有这个后缀功能。
我只是想了解为什么它不起作用,以及如何正确使用它,希望能够学习如何正确使用phpunit。
我可以看到两个潜在的原因:
1) 您的phpunit.xml
可能配置不正确。
尝试更改“testsuite”和“coverage”部分,如下所示:
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
2) 确保所有测试方法都在方法的 DocBlock 中使用 @test
注释:
/** @test */
public function your_test_method_name()
{
...
}
参见:https://phpunit.readthedocs.io/en/9.5/annotations.html
As an alternative to prefixing your test method names with test, you can use the @test annotation in a method’s DocBlock to mark it as a test method.
如果您不包含文档块,请确保您的测试方法名称以“test”开头:
public function test_method_name()
{
...
}
参见:https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html
The tests are public methods that are named test*.
无法修复后缀。我尝试重新安装 xdebug、php 和 phpstorm。正如我所说,我最终只是重命名了文件。我能找到的唯一有效解决方案。
I know, because if I rename all my test files with Test at the end instead of Tests, then they suddenly get recognized by phpunit.
这听起来像是 phpunit 的默认配置,可能表明您试图用来控制行为的配置文件 无效 。也就是说,无论你如何 运行 Phpunit,它都没有配置文件和路径,例如:
/path/to/phpunit -- /path/to/test/directory
然后 Phpunit 将使用默认配置 - 但它必须在工作目录中找到它。因此,要么配置文件的名称与 phpunit 用于自动检测的名称不同,要么它们不在工作目录中,phpunit 将在不加载配置文件的情况下启动,只执行它在具有默认后缀的目录中可以找到的内容“Test.php”。
(在 Phpstorm 中,工作目录在调用 Phpunit 时可能会有所不同,即使在同一操作的 运行 之间,但这只是一个注释。)
你可以在Phpstorm中做什么:配置Phpunit配置文件来加载。一个好的方法是在 Settings -> PHP -> 中指定默认的 Phpunit 配置文件测试运行器部分中的测试框架 默认配置文件。
另一件事是在您已经创建的 运行 操作中明确指定工作目录,但它不会执行您想要的操作,或者通过修改 Phpunit 运行 操作模板。这是模板的屏幕截图:
检查有效性的一种方法是在配置中使用bootstrap文件并使其爆炸。然后你就知道有没有效果了。
此外,如果正在使用,PHpunit 通常会写出配置文件的名称。