首页 文章

从PHPUnit代码覆盖中排除PHP接口

提问于
浏览
3

我有一个PHPUnit测试,测试一个名为HelpTokenizerTest的类 . 该类实现TokenizerInterface . 出于某些奇怪的原因,我无法从代码覆盖中排除TokenizerInterface .

尽管使用了@codeCoverageIgnore甚至@codeCoverageIgnoreStart / End,但它在代码覆盖率报告中显示为未涵盖 .

有任何想法吗?

我不希望我的测试覆盖中包含该接口,因为它没有做任何事情 . 测试接口有什么意义 .

2 回答

  • 2

    使用phpunit.xml时,您可以设置过滤器以排除具有特定名称的文件,特别是文件夹或具有特定扩展名的文件 .

    the documentation for it

    Example:

    <testsuite name="Application Test Suite">
        <directory>./application/</directory>
    </testsuite>
    
    <filter>
        <blacklist>
            HERE
        </blacklist>
           or alternatively
        <whitelist>
            <directory suffix=".php">../library/</directory>
            <directory suffix=".php">../application/</directory>
            <exclude>
                AND HERE
                <directory suffix=".phtml">../application/</directory>
            </exclude>
        </whitelist>
    </filter>
    
  • 2

    您可以在文件开头使用@codeCoverageIgnore作为注释,以获得100%0/0覆盖率 .

    <?php // @codeCoverageIgnoreStart 
    interface MyInterface
    {
        public function myfunction();
    }
    

    请注意,它在注释块中不起作用 .

    在PHPUnit的github上打开问题:https://github.com/sebastianbergmann/phpunit/issues/497

相关问题