PHPUnit始终输出“未执行测试!";在MacOS Big Sur上 [英] PHPUnit always output "No tests executed!" on MacOS Big Sur

查看:61
本文介绍了PHPUnit始终输出“未执行测试!";在MacOS Big Sur上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试运行任何 PHPUnit测试时,我总是在MacOS计算机上收到一条未执行任何测试!消息.在此特定机器上复制的一种简单方法是安装一个新的Laravel实例并运行默认测试:

When trying to run any PHPUnit tests, I always get a No tests executed! message on my MacOS machine. A simple way to reproduce, on this specific machine, is to install a fresh instance of Laravel and running the default tests :

$ composer create-project --prefer-dist laravel/laravel blog
$ cd blog
$ vendor/bin/phpunit

=> No tests executed!

预期输出为 OK(2个测试,2个断言).

据我所知,这不是PHPUnit配置问题,因为默认的Laravel代码可以正常工作,其他框架和我尝试的任何代码也会出现相同的问题strong>,相同的问题出现在不同的PHPUnit版本(8.5和9.4)上,并且上面列出的确切步骤会在Ubuntu VM以及运行Catalina的另一台Mac上返回预期的输出.

As far as I know, this is not a PHPUnit configuration issue since the default Laravel code is expected to work, the same issue appear with other framework and any code I try, the same issue is present with different PHPUnit version (8.5 and 9.4) and the exact steps listed above return the expected output inside an Ubuntu VM as well as on another Mac running Catalina.

实际上,我怀疑这不是PHPUnit问题,更多是MacOS问题 PHP配置问题,以后可能会以其他形式出现工具或项目.

Actually, I suspect it is not a PHPUnit issue, but more of a MacOS issue or a PHP configuration issue, one that could manifest in other form later with another tool or project.

PHPUnit以前可以在这台机器上正常工作,但是已经花了几周/几个月的时间才真正使用它.自从我上次成功使用Mac上的任何PHPUnit以来,唯一的变化就是升级到MacOS Big Sur并安装(然后卸载)Homebrew.

PHPUnit used to work fine on this machine, but it's been a couples weeks/months I didn't actually used it. The only thing that changed since I last (successfully) used any PHPUnit on this Mac as been upgrading to MacOS Big Sur and installing (then uninstalling) Homebrew.

问题似乎是PHPUnit找不到任何测试套件.运行 vendor/bin/phpunit --testsuite Unit 仍会输出未执行测试!,而在全新的Laravel安装中,应输出 Ok(1个测试,1个断言).

The issue appears PHPUnit can't find any testsuite. Running vendor/bin/phpunit --testsuite Unit still output No tests executed! while, inside a fresh Laravel install, should output Ok (1 test, 1 assertion).

所以我的问题是:在重新安装macOS Big Sur之前,还有其他方法可以尝试解决此问题吗?

So my question is : Is there anything else I can try to fix this before doing a fresh install of macOS Big Sur, and is anyone having the same issue?

根据评论中的要求,这是我的 phpunit.xml ,如上所述,它是

As per requested in comments, here is my phpunit.xml which is, as described above, the default 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上也可以正常工作.

Again, this shouldn't matters, as any configuration returns the same result on this computer, even some that are known to work before, on other Mac and on other OS.

使用工匠进行测试:

$ 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单元的输出:

$ 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 是上面显示的那个.

直接引用文件有效:

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

为您提供更多调试信息:

Edit #2 - Dec 1st 2020

More debug info for you :

$ 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

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单元

/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 Apple通过命名PHP 7.3.22-破坏了 version_compare (将在Big Sur中删除).安装另一个版本的PHP即可解决此问题.

tl;dr Apple broke version_compare by naming PHP 7.3.22-(to be removed in future macOS) in Big Sur. Installing another version of PHP fix this.

我找到了问题的答案.这确实是MacOS问题,与MacOS Big Sur中的内置PHP版本有关.

I found the answer to my issue. It is indeed a MacOS issue, related to the built in version of PHP in MacOS Big Sur.

经过相当多的调试(在另一个使用PHPUnit 8的项目上),我最终在这里结束: https://github.com/sebastianbergmann/phpunit/blob/ccbf3962a948112056b0eded6e4c880af4ee3695/src/Util/Configuration.php#L1041-L1055

After quite some debugging (on another project using PHPUnit 8), I endend up here : 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') return false),则return语句可以简化为:

When looking at the last line of PHPUnit code above (and since both $node->hasAttribute('phpVersion') and $node->hasAttribute('phpVersionOperator') return false), the return statement can be simplified to:

version_compare(\PHP_VERSION, \PHP_VERSION, '>=')

现在,由于Mac OS附带的PHP版本在Big Sur中已弃用,因此Apple将版本重命名为 7.3.22-(将在以后的macOS中删除).这就是导致此问题的原因,因为上面的代码现在变为:

Now, because the version of PHP that ships with MacOS got deprecated in Big Sur, Apple renamed the version to 7.3.22-(to be removed in future macOS). This is what caused the issue, as the above code now become :

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 文档中所述...

That is probably because, as explained in the official PHP Documentation...

该函数首先用点替换_,-和+.在版本字符串中并插入点.在任何非数字之前和之后,例如,"4.3.2RC1"变为"4.3.2.RC.1".然后,它会比较从左到右的部分.如果零件包含特殊版本的字符串,则按以下顺序处理:在此列表中找不到的任何字符串<开发<alpha = a<Beta = b<RC = rc<#<pl = p.这样,不仅可以比较"4.1"和"4.1.2"等不同级别的版本,而且可以比较任何包含开发状态的PHP特定版本.

The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: any string not found in this list < dev < alpha = a < beta = b < RC = rc < # < pl = p. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state.

请注意,预发行版本(例如5.3.0-dev)被认为低于最终发行版本(例如5.3.0).

Note that pre-release versions, such as 5.3.0-dev, are considered lower than their final release counterparts (like 5.3.0).

...苹果的命名方案可能被 视为劣于自身的预发布版本,而不是等同于自身.

...the naming scheme of Apple is probably treated as a pre-release version inferior to itself instead of being equal to itself.

因此,该修复程序将覆盖php版本,据我所知,这无法在全球范围内完成.使用Homebrew安装另一个版本的PHP似乎是最简单的解决方案

这篇关于PHPUnit始终输出“未执行测试!";在MacOS Big Sur上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆