尝试运行任何PHPUnit测试时,我总是No tests executed!
在MacOS计算机上收到一条消息。在此特定机器上复制的一种简单方法是安装一个新的Laravel实例并运行默认测试:
$ composer create-project --prefer-dist laravel/laravel blog
$ cd blog
$ vendor/bin/phpunit
=> No tests executed!
预期的输出将是OK (2 tests, 2 assertions)
。
据我所知,这不是一个PHPUnit配置问题,因为默认的Laravel代码可以正常工作,其他框架和我尝试的任何代码都出现相同的问题,不同的PHPUnit版本(8.5和9.4)也存在相同的问题上面列出的确切步骤将在Ubuntu VM以及另一台运行Catalina的Mac上返回预期的输出。
实际上,我怀疑这不是PHPUnit问题,而是更多的MacOS问题或PHP配置问题,以后可能在其他工具或项目中以其他形式出现。
PHPUnit以前可以在这台机器上正常工作,但是已经花了几周/几个月的时间才真正使用它。自从我上次成功使用Mac上的任何PHPUnit以来,唯一的变化就是升级到MacOS Big Sur并安装(然后卸载)Homebrew。
问题似乎是PHPUnit找不到任何测试套件。在新的Laravel安装中,应该vendor/bin/phpunit --testsuite Unit
仍输出No tests executed!
run运行输出Ok (1 test, 1 assertion)
。
所以我的问题是:在重新安装macOS Big Sur之前,我还有其他方法可以尝试解决此问题吗?有人遇到同样的问题吗?
根据评论中的要求,这是我的phpunit.xml
,如上所述,它是默认的Laravel one。
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<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>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
再次,这无关紧要,因为任何配置都会在此计算机上返回相同的结果,即使以前知道的某些配置在其他Mac和其他OS上也能返回相同的结果。
使用artisan进行测试:
$ php artisan test --testsuite Unit
No tests executed!
Time: 0.01s
输出vendor/bin/phpunit --list-suites
:
$ vendor/bin/phpunit --list-suites
PHPUnit 9.4.4 by Sebastian Bergmann and contributors.
Available test suite(s):
- Unit
- Feature
输出vendor/bin/phpunit -v --testsuite Unit
:
$ vendor/bin/phpunit -v --testsuite Unit
PHPUnit 9.4.4 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.22-(to be removed in future macOS)
Configuration: /Users/malou/Desktop/blog/phpunit.xml
No tests executed!
注意:
/Users/malou/Desktop/blog/phpunit.xml
是上面显示的那个。
直接引用文件有效:
$ vendor/bin/phpunit ./tests/Unit
PHPUnit 9.4.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.006, Memory: 8.00 MB
OK (1 test, 1 assertion)
为您提供更多调试信息:
$ php -v
WARNING: PHP is not recommended\nPHP is included in macOS for compatibility with legacy software.\nFuture versions of macOS will not include PHP.
PHP 7.3.22-(to be removed in future macOS) (cli) (built: Oct 30 2020 00:19:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.22, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.0, Copyright (c) 2002-2019, by Derick Rethans
MacOS Big Sur 11.0.1
作为响应,PHPUnit始终输出“未执行测试!” 在MacOS Big Sur上:
我添加了以下内容:
public static function main(bool $exit = true): int
{
var_dump(ini_get("auto_prepend_file"));
var_dump($_SERVER['argv']); die;
return (new static)->run($_SERVER['argv'], $exit);
}
执行时vendor/bin/phpunit
:
/Users/malou/Desktop/blog/vendor/phpunit/phpunit/src/TextUI/Command.php:163:
string(0) ""
/Users/malou/Desktop/blog/vendor/phpunit/phpunit/src/TextUI/Command.php:164:
array(1) {
[0] =>
string(18) "vendor/bin/phpunit"
}
执行时 vendor/bin/phpunit --testsuite Unit
/Users/malou/Desktop/blog/vendor/phpunit/phpunit/src/TextUI/Command.php:163:
string(0) ""
/Users/malou/Desktop/blog/vendor/phpunit/phpunit/src/TextUI/Command.php:164:
array(3) {
[0] =>
string(18) "vendor/bin/phpunit"
[1] =>
string(11) "--testsuite"
[2] =>
string(4) "Unit"
}
tl; dr 苹果version_compare
因7.3.22-(to be removed in future macOS)
在Big Sur中命名PHP而破产。安装另一个版本的PHP可以解决此问题。
我找到了问题的答案。这确实是MacOS问题,与MacOS Big Sur中的内置PHP版本有关。
经过相当多的调试(在另一个使用PHPUnit 8的项目上)之后,我在这里结束:https : //github.com/sebastianbergmann/phpunit/blob/ccbf3962a948112056b0eded6e4c880af4ee3695/src/Util/Configuration.php#L1041-L1055
private function satisfiesPhpVersion(DOMElement $node): bool
{
$phpVersion = \PHP_VERSION;
$phpVersionOperator = '>=';
if ($node->hasAttribute('phpVersion')) {
$phpVersion = (string) $node->getAttribute('phpVersion');
}
if ($node->hasAttribute('phpVersionOperator')) {
$phpVersionOperator = (string) $node->getAttribute('phpVersionOperator');
}
return \version_compare(\PHP_VERSION, $phpVersion, (new VersionComparisonOperator($phpVersionOperator))->asString());
}
当在PHPUnit的代码之上(并因为这两个最后一行寻找$node->hasAttribute('phpVersion')
并$node->hasAttribute('phpVersionOperator')
返回false),return语句可以简化为:
version_compare(\PHP_VERSION, \PHP_VERSION, '>=')
现在,由于Big Sur中不推荐使用MacOS附带的PHP版本,因此Apple将版本重命名为7.3.22-(to be removed in future macOS)
。这就是导致此问题的原因,因为上面的代码现在变为:
version_compare("7.3.22-(to be removed in future macOS)", "7.3.22-(to be removed in future macOS)", '>=')
其中返回false而不是true。
一种简单的测试方法:
$foo = version_compare("7.3.22-(to be removed in future macOS)", "7.3.22-(to be removed in future macOS)", '>=');
var_dump($foo); // bool(false)
$bar = version_compare("7.3.22", "7.3.22", '>=');
var_dump($bar); // bool(true)
这可能是因为,如官方PHP文档中所述...
该函数首先用点替换_,-和+。在版本字符串中,并插入点。在任何非数字之前和之后,例如“ 4.3.2RC1”变为“ 4.3.2.RC.1”。然后,它会比较从左到右的部分。如果零件包含特殊版本的字符串,则将按以下顺序处理:在此列表中找不到的任何字符串<dev <alpha = a <beta = b <RC = rc <#<pl = p。这样,不仅可以比较“ 4.1”和“ 4.1.2”等不同级别的版本,而且可以比较任何包含开发状态的特定于PHP的版本。
请注意,预发行版本(例如5.3.0-dev)被认为低于其最终发行版本(例如5.3.0)。
...苹果的命名方案可能被视为劣于自身的预发布版本,而不是等同于自身。
因此,该修复程序将覆盖php版本,据我所知,这无法在全球范围内完成。使用Homebrew安装另一个版本的PHP似乎是最简单的解决方案
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句