首页 > 解决方案 > 带有 Clover 代码覆盖率报告的 PHPUnit:测试未执行且命令退出

问题描述

我“继承”了一个非常古老的 PHP 项目,仅与 PHP 5.6 兼容,并且没有任何框架编写,我正在尝试为 PHP 7.x “现代化”它,并添加一些单元测试和一些代码检查工具,如带有 Jenkins 的 SonarQube。

该项目有一些可执行脚本,需要一些参数才能正确执行,当我执行 phpunit 并启用 Clover 覆盖的生成时,它会尝试执行它们,但由于缺少所需的参数而失败:

⋊> ~/P/myproject on mybranch ⨯ ./phpunit tests/
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.5 with Xdebug 2.9.5
Configuration: /Users/me/Projects/myproject/phpunit.xml


You must specify a source (--source)!

这是files-index.php项目根文件夹中命令的输出,需要一个--source参数才能运行:

[...]

if (!$args['source']) {
    exit("You must specify a source (--source)!\n");
}

[...]

我不明白的是为什么如果我在没有启用覆盖率报告的情况下执行 PHPUnit,项目中存在的(少数)测试执行时没有错误(无论如何文件files-index.php目前没有测试):

⋊> ~/P/myproject on mybranch ⨯ ./phpunit --no-config tests/
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

........                                                            8 / 8 (100%)

Time: 194 ms, Memory: 4.00MB

OK (8 tests, 8 assertions)

这是我的phpunit.xml文件:

⋊> ~/P/myproject on mybranch ⨯ cat phpunit.xml                                                                                               22:10:25
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="phpunit.xsd"
         backupGlobals="false"
         verbose="true">

    <testsuites>
        <testsuite name="My Project">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">.</directory>
            <exclude>
                <directory suffix=".php">./vendor</directory>
                <directory suffix=".php">./_classes</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="junit" target="./reports/logfile.xml"/>
        <log type="coverage-clover" target="./reports/coverage.xml"/>
    </logging>

    <php>
        <const name="PHPUNIT_TESTSUITE" value="true"/>
    </php>
</phpunit>

我哪里错了?

标签: phpphpunitcode-coveragecloverphp-code-coverage

解决方案


推荐阅读