gpt4 book ai didi

testing - SimpleTest 中的代码覆盖率

转载 作者:行者123 更新时间:2023-11-28 20:28:18 25 4
gpt4 key购买 nike

在使用类似于 PHPUnit 的 SimpleTest 时,有什么方法可以生成代码覆盖率报告。

我已经在他们的网站上阅读了 SimpleTest 的文档,但找不到关于如何做的明确方法!我遇到了这个 website就是说

we can add require_once (dirname(__FILE__).'/coverage.php') to the intended file and it should generate the report, but it did not work!

如果有关于如何生成代码覆盖率的有用网站,请在这里分享。

非常感谢。

最佳答案

我也无法让它以官方支持的方式工作,但这是我正在工作的东西,我能够通过检查他们的代码来破解。这适用于 SimpleTest 的 v1.1.7,而不是他们的主代码。在撰写本文时,v1.1.7 是最新版本,适用于新版本的 PHP 7,尽管它是旧版本。

首先你必须确保你有 Xdebug安装、配置和工作。在我的系统上有 php.ini 文件的 CLI 和 Apache 版本,必须正确配置这取决于我是尝试通过 Apache 还是直接从终端使用 PHP。有 Xdebug 的替代品,但大多数人使用 Xdebug。

然后,你必须制作 PHP_CodeCoverage可从您的代码访问的库。我建议将它作为 composer 添加到您的项目中包。

现在您只需手动使用该库来捕获代码覆盖率并生成报告。具体怎么做取决于您运行测试的方式。就个人而言,我在终端上运行我的测试,并且我有一个 php 在启动脚本之前运行的引导文件。在 bootstrap 文件的末尾,我包含了 SimpleTest 自动运行文件,因此它将自动运行包含在内的任何测试类中的测试,如下所示:

require_once __DIR__.'/vendor/simpletest/simpletest/autorun.php';

在引导文件中的某处,您需要创建一个过滤器,将要报告的目录和文件列入白名单,创建一个覆盖对象并将过滤器传递给构造函数,启动覆盖,并创建并注册一个关闭函数这将改变 SimpleTest 执行测试的方式,以确保它也停止覆盖并生成覆盖报告。您的 Bootstrap 文件可能如下所示:

<?php
require __DIR__.'/vendor/autoload.php';
$filter = new \SebastianBergmann\CodeCoverage\Filter();
$filter->addDirectoryToWhitelist(__DIR__."/src/");
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage(null, $filter);
$coverage->start('<name of test>');

function shutdownWithCoverage($coverage)
{
$autorun = function_exists('\run_local_tests'); // provided by simpletest
if ($autorun) {
$result = \run_local_tests(); // this actually runs the tests
}
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, __DIR__.'/tmp/code-coverage-report');
if ($autorun) {
// prevent tests from running twice:
exit($result ? 0 : 1);
}
}
register_shutdown_function('\shutdownWithCoverage', $coverage);
require_once __DIR__.'/vendor/simpletest/simpletest/autorun.php';

关于testing - SimpleTest 中的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43081326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com