在我的MacOS机器上,尝试运行 任何 PHPUnit 测试时,我总是会得到一个 No tests executed!
的消息。在这台特定的机器上,重现的简单方法是安装 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找不到任何测试套件。运行vendor/bin/phpunit --testsuite Unit
仍然输出No tests executed!
,而在新的Laravel安装中,应该输出Ok (1 test, 1 assertion)
。
因此,我的问题是:在重新安装macOS Big Sur之前,我还能尝试什么来解决这个问题,是否有人遇到相同的问题?
编辑- 2020年12月1日
根据评论中的要求,这里是我的phpunit.xml
,正如上面所述,这是默认的Laravel配置文件。
<?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和其他操作系统上运行良好的配置也是如此。
使用 artisan 运行测试:
$ php artisan test --testsuite Unit
No tests executed!
Time: 0.01s
< p >运行命令 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!
N.B.: /Users/malou/Desktop/blog/phpunit.xml
is the one displayed above.
直接引用文件可行:
$ 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)
编辑#2 - 2020年12月1日
以下是更多的调试信息:
$ 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
Edit - Dec 2nd 2020
In response to PHPUnit always output "No tests executed!" on MacOS Big Sur:
I added the following:
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"
}
vendor/bin/phpunit --list-suites
输出什么?而vendor/bin/phpunit -v --testsuite Unit
配置文件是phpunit.xml
吗? - Total PusherPHPUnit\TextUI\Command::main();
中我发现了这个:return (new static)->run($_SERVER['argv'], $exit);
。请添加var_dump(ini_get("auto_prepend_file")); var_dump($_SERVER['argv']); die;
并显示结果。 - Total Pusher