为什么 PHPUnit 在生成覆盖率报告时要执行代码?

发布于 2024-10-04 11:27:13 字数 1584 浏览 0 评论 0 原文

亲爱的叠花, 我们正在开发一个基于 cakephp 的 Web 应用程序。事实证明,以 TDD 方式使用 CakePHP 有点困难,因此我们尝试通过将所有业务逻辑提取到不依赖 cakephp 的类,在框架本身上开发尽可能少的代码。因此,我们能够使用 phpunit 测试我们的库,并且问题最少。然而,我们确实希望在覆盖率报告中包含未经测试的代码,以密切关注 cake 和我们无法测试的库之间的粘合代码量。问题是,当告诉 phpunit 解释这些代码时,它会疯狂地解析和执行 cakephp 的代码,并且严重崩溃。 我的问题是:为什么 phpunit 会执行这段代码?这里有我们不理解或做错的事情吗? 这是我们正在使用的 phpunit.xml 文件:

<?xml version="1.0" encoding="utf-8" ?>

<phpunit backupGlobals="true"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
  <filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
      <directory suffix=".php">app</directory>
      <exclude>
        <directory suffix=".php">tests</directory>
        <directory suffix=".php">app/webroot</directory>
        <directory suffix=".php">app/plugins</directory>
        <directory suffix=".php">app/vendors</directory>
        <directory suffix=".php">app/config</directory>
        <directory suffix=".php">app/tmp</directory>
        <directory suffix=".php">cake</directory>
        <directory suffix=".php">vendors</directory>
      </exclude>
    </whitelist>
  </filter>
</phpunit>

感谢您的帮助。

Dear stackoverflowers,
We are developing a web application based on cakephp. CakePHP turns out to be a bit hard to use in a TDD manner and therefore we have are trying to develop the least amount of code possible on the framework it self by extracting all business logic out to classes that do not depend on cakephp. As such, we are able to test our libraries using phpunit with minimal problems. However, we do want to included the untested code in our coverage report more than anything to keep an eye on the amount of glue code between cake and our libraries that we can not test. The problem is then that when telling phpunit to account for these code it goes crazy parsing and executing cakephp's code and it breaks miserably.
My question is: Why is phpunit executing this code at all? Is there something we are not understanding or doing wrong here?
Here is the phpunit.xml file we are using:

<?xml version="1.0" encoding="utf-8" ?>

<phpunit backupGlobals="true"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
  <filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
      <directory suffix=".php">app</directory>
      <exclude>
        <directory suffix=".php">tests</directory>
        <directory suffix=".php">app/webroot</directory>
        <directory suffix=".php">app/plugins</directory>
        <directory suffix=".php">app/vendors</directory>
        <directory suffix=".php">app/config</directory>
        <directory suffix=".php">app/tmp</directory>
        <directory suffix=".php">cake</directory>
        <directory suffix=".php">vendors</directory>
      </exclude>
    </whitelist>
  </filter>
</phpunit>

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-10-11 11:27:13

您需要将 cakephp 文件添加到 黑名单。您应该能够在 xml 配置文件中执行此操作:

<filter>
  <blacklist>
    <directory suffix=".php">/path/to/files</directory>
    <file>/path/to/file</file>
    <exclude>
      <directory suffix=".php">/path/to/files</directory>
      <file>/path/to/file</file>
    </exclude>
  </blacklist>
</filter>

有更多信息 此处

You need to add the cakephp files to the blacklist. You should be able to do this in your xml config file:

<filter>
  <blacklist>
    <directory suffix=".php">/path/to/files</directory>
    <file>/path/to/file</file>
    <exclude>
      <directory suffix=".php">/path/to/files</directory>
      <file>/path/to/file</file>
    </exclude>
  </blacklist>
</filter>

There is further information here

岁吢 2024-10-11 11:27:13

为什么 phpunit 会执行这段代码?

它这样做是因为它还需要获取有关未涵盖的类、方法和函数的信息。它包含它找到的文件,并使用 Reflection 来发现有关类的所有信息。这比手动解析和分析 PHP 文件的解析的标记更容易。

Why is phpunit executing this code at all?

It does this because it needs to get information about the classes, methods and functions that are not covered, too. It includes the files it finds and uses Reflection to discover all information about the classes. That's easier than manually parsing and analyzing the parsed tokens of a PHP file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文