先安装xdebug
参考:https://blog.csdn.net/nece001/article/details/112713712
不管哪种方式xdebug都是必须的!
xdebug的这一项配置必须要有coverage,逗号间不能有空格!
xdebug.mode = debug,coverage
代码覆盖率
使用PHPUnit生成
参考:PHP 单元测试覆盖率 | Laravel China 社区
安装PHPUnit
phpunit官网:PHPUnit – PHP测试框架
安装参考:PHPUnit 手册 – 第 1 章 安装 PHPUnit
项目根目录新建phpunit.xml
<!--
属性
bootstrap 需要前置加载的文件,如:autoload.php
-->
<phpunit bootstrap="">
<!-- 指定测试代码所在目录(相对于本文件所在位置的路径) -->
<testsuites>
<testsuite name="mySuite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<!-- 需要计算覆盖率的目录 -->
<filter>
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./extend</directory>
</whitelist>
</filter>
<!-- 生成覆盖率报告的格式和位置 -->
<logging>
<log type="coverage-html" target="./test-result/report"/>
</logging>
</phpunit>
编写测试代码
- 新建extend目录,用于存放被测代码。
- 新建tests目录,用于存放单元测试代码。
被测代码:extend\helper\Math.php
<?php
class Math
{
public function add($a, $b)
{
return $a + $b;
}
public function jian($a, $b)
{
return $a - $b;
}
public function cheng($a, $b)
{
return $a * $b;
}
public function chu($a, $b)
{
return $a / $b;
}
public function call($a, $b)
{
$this->add($a, $b);
}
}
测试代码:
tests\simple\MathTest.php
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . '/../../extend/helper/Math.php');
class MathTest extends TestCase
{
public function testAdd()
{
$math = new Math();
$result = $math->add(1, 2);
$this->assertEquals(3, $result, '当期望的数组和实际的数组不相等时报这条信息');
}
public function testSub()
{
$math = new Math();
$result = $math->jian(5, 3);
$this->assertEquals(2, $result, '当期望的数组和实际的数组不相等时报这条信息');
}
public function testCheng()
{
$math = new Math();
$result = $math->cheng(5, 3);
$this->assertEquals(15, $result, '当期望的数组和实际的数组不相等时报这条信息');
}
}
tests\simple\Math2Test.php
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . '/../../extend/helper/Math.php');
class Math2Test extends TestCase
{
public function testChu()
{
$math = new Math();
$result = $math->chu(1, 2);
$this->assertEquals(3, $result, '当期望的数组和实际的数组不相等时报这条信息');
}
public function testCall()
{
$math = new Math();
$result = $math->call(1, 2);
$this->assertEquals(3, $result, '当期望的数组和实际的数组不相等时报这条信息');
}
}
运行
d:/项目根目录> phpunit
运行后会生成报告放在目录:test-result\report ,查看index.html
另一种方式
参考文档:(代码已过时,我在下面做了修改)
https://blog.csdn.net/galen2016/article/details/82791856
安装php-code-coverage
代码仓库:https://github.com/sebastianbergmann/php-code-coverage
安装命令:
composer require phpunit/php-code-coverage
在根目录新增一个prepend.php
<?php
/**
* Created by PhpStorm.
* User: 灵枢
* Date: 2018/9/5
* Time: 下午5:02
*/
require_once dirname(__FILE__) . '/vendor/autoload.php';
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Xdebug3Driver;
use SebastianBergmann\CodeCoverage\Filter;
$filter = new Filter;
$filter->includeDirectory('/path/to/directory');
// 实例初始化做了修改
$coverage = new CodeCoverage(
(new Xdebug3Driver($filter)),
$filter
);
$coverage->start('<Site coverage>'); #开始统计
register_shutdown_function('__coverage_stop', $coverage); #注册关闭方法
function __coverage_stop(CodeCoverage $coverage)
{
$coverage->stop(); #停止统计
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
# 设置生成代码覆盖率页面的路径
$writer->process($coverage, dirname(__FILE__) . '/coverage_html');
}
新建文件echoNumber.php,用来测试的覆盖率
<?php
/**
* Created by PhpStorm.
* User: 灵枢
* Date: 2018/8/31
* Time: 下午3:18
*/
include_once("prepend.php");
class echoNumber
{
function add($a, $b)
{
echo $a + $b . PHP_EOL;
}
function jian($a, $b)
{
echo $a - $b . PHP_EOL;
}
function cheng($a, $b)
{
echo $a * $b . PHP_EOL;
}
function chu($a, $b)
{
echo $a / $b . PHP_EOL;
}
function call($a, $b)
{
$this->add($a, $b);
}
}
$f = new echoNumber();
$f->add(3, 4);
$f->cheng(2, 3);
$f->call(5, 6);
运行命令:
d:> php echoNumber.php
运行后就会在根目录下新增一个文件夹,用来存放生成html报告