PHPUnit 代码覆盖率

2021-12-29 00:00:00 code-coverage php zend-framework phpunit

我正在学习单元测试 Zend Framework 应用程序.到目前为止,我已经设置了 PHPUnit 以使用 Zend Framework 并开始编写一些简单的测试用例.

I am learning the ropes with Unit testing Zend Framework applications. So far I have set up PHPUnit to work with Zend Framework and have started writing some simple Test Cases.

我的问题是,我想知道为什么 Code Coverage 尽管在我的 phpunit.xml 的日志记录标签中设置但不起作用.

My issue is that I am wondering why Code Coverage does not work in spite of being set in the logging tag in my phpunit.xml.

我没有收到任何错误,但没有生成覆盖率报告.

I don't get any error but no coverage report is generated.

但是当我运行 phpunit --coverage <dir>

我的phpunit的日志部分如下:

The logging section of my phpunit is as below:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
        <testsuite name="CI Test Suite">
            <directory>./</directory>
        </testsuite>
        <testsuite name="Library Test Suite">
            <directory>./library</directory>
        </testsuite>

        <filter>
            <whitelist>
                <directory suffix=".php">../application/</directory>
                <exclude>
                    <directory suffix=".phtml">../application</directory>
                    <file>../application/Bootstrap.php</file>
                    <file>../application/controllers/ErrorController.php</file>
                </exclude>
            </whitelist>
           <logging>
               <log type="coverage-html" target="./log/report" charset="UTF-8" yui="true"
   highlight="true" lowUpperBound="50" highLowerBound="80" />
               <log type="testdox" target="./log/testdox.html" />    
           </logging>
        </filter>
    </phpunit>

有人遇到过这种情况吗?那么可能的问题是什么?

Anyone encounter this before? What is then likely problem?

推荐答案

这是我的一个项目的 phpunit.xml,这很好用.如您所见,日志记录部分在过滤器部分之外,因此这可能是 Mark Ba​​ker 评论的问题.我选择了这个,因为它来自一个小项目,非常简单.

Here is the phpunit.xml for one of my projects, this works fine. As you can see, the logging section is outside the filter section, so that is probably your issue as commented by Mark Baker. I chose this one as it is from a small project and is very simple.

<phpunit bootstrap="./bootstrap.php" colors="false">
    <testsuite name="HSSTests">
        <directory>./</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">d:/wamp/app_hss/</directory>
            <exclude>
                <directory suffix=".phtml">d:/wamp/app_hss/</directory>
                <directory suffix=".php">d:/wamp/app_hss/tests/</directory>
            </exclude>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
            yui="true" highlight="true"
            lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html" />
    </logging>
</phpunit>

您可能需要的所有信息都在 PHPunit 手册中.

All the information you could ever need on this is in the PHPunit manual.

相关文章