PHPUnit袖珍指南 第十章 代码覆盖率分析
来源:互联网 发布:眼睑凹陷填充 知乎 编辑:程序博客网 时间:2024/04/27 23:05
第十章 代码覆盖率分析
你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。代码覆盖率分析告诉你当测试进行时,那些产品代码执行过了。
PHPUnit的代码覆盖率分析应用了Xdebug[6]扩展提供的语句覆盖率功能。 什么时语句覆盖率?举个例子来说,如果有一个方法有100个代码行,在测试进行时,只有75行真正运行了,这个方法的语句覆盖率就是75%。
[6] http://www.xdebug.org/
图1 显示了BankAccount(参见例12)的代码覆盖率报告。此HTML格式的报告是由PHPUnit命令行测试运行器生成的,使用--coverage-html选项。 黑字部分表示可执行的代码,灰字部分表示不可执行的代码,高亮代码行部分表示执行过的代码。
图1-1. BankAccount类没有被测试完全覆盖。
此代码覆盖率报告表示,我们要增加代码覆盖率的话,就需要增加setBalance(),depositMoney()和withdrawMoney()的测试,并使用合法的值。例14 显示了如何增加BankAccountTest类的测试用例来提高BankAccount类的代码覆盖率。
例14.用测试覆盖BankAccount类
<?php
require_once 'PHPUnit2/Framework/TestCase.php';
require_once 'BankAccount.php';
class BankAccountTest extends PHPUnit2_Framework_TestCase {
// …
public function testSetBalance( ) {
$this->ba->setBalance(1);
$this->assertEquals(1, $this->ba->getBalance( ));
}
public function testDepositAndWidthdrawMoney( ) {
$this->ba->depositMoney(1);
$this->assertEquals(1, $this->ba->getBalance( ));
$this->ba->withdrawMoney(1);
$this->assertEquals(0, $this->ba->getBalance( ));
}
}
?>
见图2,我们看到类BankAccount已经被测试完全覆盖了。
图1-2
在本书后“PHPUnit 和Phing”一章中,你将学会怎么使用Phing生成更加详细的代码覆盖率报告。
--------------------------------------------------------------------------------------------------------------------
原文:
Chapter 10. Code-Coverage Analysis
You have learned how to use unit tests to test your code. But how do you test your tests? How do you find code that is not yet testedor, in other words, not yet covered by a test? How do you measure testing completeness? All these questions are answered by a practice called code-coverage analysis. Code-coverage analysis gives you an insight into what parts of the production code are executed when the tests are run.
PHPUnit's code-coverage analysis utilizes the statement coverage functionality provided by the Xdebug[6] extension. An example of what statement coverage means is that if there is a method with 100 lines of code, and only 75 of these lines are actually executed when tests are being run, then the method is considered to have a code overage of 75 percent.
[6] http://www.xdebug.org/
Figure 1 shows a code-coverage report for the BankAccount class (from Example 12) in HTML format generated by the PHPUnit command-line test runner's --coverage-html switch. Executable code lines are black; non-executable code lines are gray. Code lines that are actually executed are highlighted.
Figure 1-1. The BankAccount class, not completely covered by tests
The code-coverage report shows that we need to write tests that call setBalance( ), depositMoney( ), and withdrawMoney( ) with legal values in order to achieve complete code coverage. Example 14 shows tests that need to be added to the BankAccountTest test-case class to completely cover the BankAccount class.
Example 14. The BankAccount class, covered by tests
<?php
require_once 'PHPUnit2/Framework/TestCase.php';
require_once 'BankAccount.php';
class BankAccountTest extends PHPUnit2_Framework_TestCase {
// …
public function testSetBalance( ) {
$this->ba->setBalance(1);
$this->assertEquals(1, $this->ba->getBalance( ));
}
public function testDepositAndWidthdrawMoney( ) {
$this->ba->depositMoney(1);
$this->assertEquals(1, $this->ba->getBalance( ));
$this->ba->withdrawMoney(1);
$this->assertEquals(0, $this->ba->getBalance( ));
}
}
?>
In Figure 2, we see that the BankAccount class is now covered completely by tests.
In the "PHPUnit and Phing" section, later in this book, you will learn how to use Phing to generate more detailed code-coverage reports.
- PHPUnit袖珍指南 第十章 代码覆盖率分析
- PHPUnit袖珍指南 第十章 代码覆盖率分析
- PHPUnit袖珍指南-第四章 安装PHPUnit
- PHPUnit袖珍指南-第四章 安装PHPUnit
- PHPUnit袖珍指南-第二章 自动测试
- PHPUnit袖珍指南 第六章 装置器
- PHPUnit袖珍指南 第八章 未完成测试
- PHPUnit袖珍指南 第十一章 残根
- PHPUnit袖珍指南-第二章 自动测试
- PHPUnit袖珍指南 第六章 装置器
- PHPUnit袖珍指南 第八章 未完成测试
- PHPUnit袖珍指南 第十一章 残根
- PHPUnit袖珍指南-概述
- PHPUnit袖珍指南-概述
- PHPUnit袖珍指南-第三章 PHPUnit的目的
- PHPUnit袖珍指南-第三章 PHPUnit的目的
- PHPUnit袖珍指南-第一章 简介
- PHPUnit袖珍指南-第一章 简介
- 在 Android 程序中禁止屏幕旋转和避免重启Activity
- PHPUnit袖珍指南 第八章 未完成测试
- ETL见解
- PHPUnit袖珍指南 第九章 测试优先编程
- Valgrind使用以及Linux下调试技术
- PHPUnit袖珍指南 第十章 代码覆盖率分析
- 小心C语言时间函数陷阱
- PHPUnit袖珍指南 第十一章 残根
- Android屏幕旋转时Activity的生命周期
- 关于上传文件验证是否安全
- 创建和使用静态库 (C++)
- PHPUnit袖珍指南 第十二章 测试的其他用途
- XAF之更丰富的状态栏
- opencv获取视频彩色图像变成灰度图像出现倒立的情况分析