PHPUnit袖珍指南 第十章 代码覆盖率分析
2006-05-31 21:05
597 查看
第十章代码覆盖率分析
你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。代码覆盖率分析告诉你当测试进行时,那些产品代码执行过了。
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的代码覆盖率分析应用了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袖珍指南 第十章 代码覆盖率分析
- 深入Atlas系列:客户端代码编写规则分析与指南
- ant中使用cobertura分析测试用例的代码覆盖率
- 代码覆盖率分析 - VectorCAST/CBA
- 使用 Ratinal Purecoverage 进行 C++ 代码覆盖率分析
- 代码覆盖率分析(gcov)
- 使用代码覆盖率分析度量测试的完整性
- [Android]使用Emma进行单体测试代码覆盖率分析
- Linux平台代码覆盖率测试工具GCOV相关文件分析
- 代码覆盖率分析工具--NCover
- AQtime代码覆盖率深度分析之二
- JAVA代码覆盖率工具JaCoCo-原理简单分析
- VS2013单元测试及代码覆盖率分析--Xunit
- Jacoco & Cobertura代码覆盖率分析对比
- jmeter emma.jar 采用EMMA对JMeter执行代码覆盖率分析
- 代码覆盖率分析
- 使用VS2005进行代码覆盖率分析
- c#编程指南(十六) 连续两周出现在CodeProject Newsletter上代码分析
- (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- gcov测试代码覆盖率及分析代码性能