如何使 PHPUnit 忽略文件模式


How can I make PHPUnit ignore a file pattern?

我正在为一个项目做代码覆盖率报告,并且在测试运行时包含或需要大量文件,这些文件实际上并不需要测试或添加到覆盖率报告中(我使用的是Zend Framework 2;config + Module文件是这里的罪魁祸首)。

有没有一种简单的方法可以简单地忽略文件模式modules/*/Module.php

这是我的黑名单:

<filter>
    <blacklist>
        <directory>config</directory>
        <directory>vendor</directory>
        <directory>module/*/config</directory>
    </blacklist>
</filter>

但是,添加<file>module/*/Module.php</file>对 html 代码覆盖率报告没有影响;添加它对在覆盖率报告中包括Module.php文件没有任何影响。

Zend 框架在 phpunit bootstrap.php 文件中启动,通常使用

Application::init(require "config/application.test.php")

除了将每个模块的Module.php文件添加到黑名单之外,PHPUnit 有什么方法可以正确地做到这一点吗?我不是在 PHPUnit 的测试用例中寻找使用 setUp 方法的答案;我正在寻找配置。

我使用的是 PHPUnit 4.7 + 4.8。

因此,为了排除具有相同名称的多个文件,<file>通常不会做任何事情,因为它只会完全匹配单个文件。

解决方案是使用 <directory> ,但使用后缀属性;

<phpunit
    bootstrap="tests/bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true">
    <testsuites>
        <testsuite name="Test Suite">
            <!-- functional tests -->
            <directory>tests</directory>
            <!-- unit tests -->
            <directory>module/*/test</directory>
        </testsuite>
    </testsuites>
    <!-- ignore folders for code coverage -->
    <filter>
        <blacklist>
            <directory>config</directory>
            <directory>vendor</directory>
            <directory>module/*/config</directory>
            <directory suffix="Module.php">module/*</directory>
        </blacklist>
    </filter>
</phpunit>

<directory suffix="Module.php">module/*</directory>将匹配模块目录顶层中以 Module.php 结尾的多个文件,包括Module.php本身。

由于没有理由将任何其他 php 文件放在 Zend Framework 2 的该目录中,因此这似乎是最好的方法。

你在哪里添加了你的文件标签?

这是我blacklist标签,它对我有用。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="Bootstrap.php"
    colors="true"
    stopOnError="true"
    stopOnFailure="false"
    strict="true"
    verbose="true">
    <testsuites>
        <testsuite name="All Modules">
            <directory>../module/*/tests/*Test</directory>
        </testsuite>
    </testsuites>
   <filter>
        <blacklist>
            <directory suffix=".php">../vendor</directory>
            <directory suffix=".php">../config</directory>
            <directory suffix=".php">../data</directory>
            <directory suffix=".php">../module/*/src/*/Controller</directory>
            <file>../module/*/Module.php</file>
            <file>../module/*/src/*/Factory/*ControllerFactory.php</file>
            <directory suffix=".php">../module/*/tests/*</directory>
        </blacklist>
    </filter>
    <php>
        <ini name="memory_limit" value="2047M" />
    </php>
    <logging>
      <log type="coverage-html" target="../build/tests/coverage/" charset="UTF-8"
           yui="true" highlight="false"
           lowUpperBound="35" highLowerBound="70"/>
      <log type="coverage-clover" target="../build/tests/clover.xml"/>
      <log type="junit" target="../build/tests/junit.xml" logIncompleteSkipped="false"/>
    </logging>
</phpunit>

我有这个结构:

- module
- vendor
- tests
  - phpunit.xml
- public
- build.xml