【问题标题】:Define the relative path when clover.xml is generated in PHPUnit?PHPUnit中生成clover.xml时定义相对路径?
【发布时间】:2020-03-05 00:34:43
【问题描述】:

我正在使用 PHPUnit 在我的 WordPress 插件上运行集成测试,当我生成覆盖率和 clover.xml 文件时,clover.xml 中的文件名属性将具有我的文件的绝对路径,例如

<file name="/Users/denis/vagrant-local/www/project/public_html/wp-content/plguins/my-plugin/file-that-is-tested.php">

由于我需要将此文件发送到 SonarQube,因此每次发送到 SonarQube 时都需要修改此文件,以便我只有相对路径(从 wp-config 文件夹开始)

<file name="wp-content/plguins/my-plugin/file-that-is-tested.php">

如果我发送第一个版本,SonarQube 会将代码覆盖率报告为 0.0%,如果我发送另一个版本,它将显示一些覆盖率(它与 PHPUnit 创建的版本不同,但这并不重要)。

有没有办法在 PHPUnit 配置中指定这个文件名属性,还是我每次运行测试时都需要运行一个 bash 脚本来删除这个额外的部分?

编辑

phpunit.xml.dist 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
  backupGlobals="false"
  backupStaticAttributes="false"
  beStrictAboutCoversAnnotation="true"
  bootstrap="tests/bootstrap.php"
  colors="true"
  convertErrorsToExceptions="true"
  convertNoticesToExceptions="true"
  convertWarningsToExceptions="true"
  printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
  stopOnError="false"
  stopOnFailure="false"
  stopOnIncomplete="false"
  stopOnSkipped="false"
  verbose="true"
  >
  <testsuites>
    <testsuite name="integration">
      <directory prefix="test-" suffix=".php">./tests/integration/</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist>
      <directory>./</directory>
      <exclude>
        <directory>./node_modules</directory>
        <directory>./vendor</directory>
        <directory>./tests</directory>
        <directory suffix=".php">./src/old</directory>
      </exclude>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-html" target="tests/_reports/coverage" lowUpperBound="35" highLowerBound="80" />
    <log type="coverage-clover" target="tests/_reports/clover.xml"/>
    <log type="coverage-php" target="tests/_reports/logs/coverage.cov"/>
    <log type="junit" target="tests/_reports/logs/logfile.xml"/>
  </logging>
</phpunit>

【问题讨论】:

  • SonarQube 代理在哪里运行?你能分享你的 phpunit.xml 配置文件中的相关部分吗?
  • SonarQube 扫描发生在我的 Jenkins 机器上,但我在本地运行测试。我会将phpunit.xml.dist 放在问题中。
  • 你找到解决这个问题的方法了吗?我在 docker 容器中运行测试,所以我还必须操作这些路径。

标签: php wordpress sonarqube phpunit


【解决方案1】:

我创建了一个 Symfony 命令,它基本上只是搜索替换绝对路径的一部分并将其删除

https://github.com/dingo-d/woo-solo-api/tree/develop/bin

然后你可以在你的作曲家脚本中使用它

    "scripts": {
        "test:coverage": [
            "@php ./vendor/bin/codecept run Integration --coverage --coverage-xml --coverage-html --ansi",
            "@php bin/console clean-coverage /Users/denis.zoljom/Sites/personal/plugins-testing/ --ansi"
        ]
    },

或您正在使用的任何套件。第二个命令将删除coverage.serializedcoverage.xml 文件中路径的绝对部分。

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2014-05-07
    • 2017-10-04
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多