PHPUnit袖珍指南 第十章 代码覆盖率分析

第十章 代码覆盖率分析

你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。代码覆盖率分析告诉你当测试进行时,那些产品代码执行过了。

PHPUnit的代码覆盖率分析应用了Xdebug[6]扩展提供的语句覆盖率功能。 什么时语句覆盖率?举个例子来说,如果有一个方法有100个代码行,在测试进行时,只有75行真正运行了,这个方法的语句覆盖率就是75%。

[6] http://www.xdebug.org/

图1

1 显示了BankAccount(参见例12)的代码覆盖率报告。此HTML格式的报告是由PHPUnit命令行测试运行器生成的,使用--coverage-html选项。 黑字部分表示可执行的代码,灰字部分表示不可执行的代码,高亮代码行部分表示执行过的代码。

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 697.5pt; HEIGHT: 287.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CSUMMER~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

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已经被测试完全覆盖了。

<shape id="_x0000_i1026" style="WIDTH: 697.5pt; HEIGHT: 283.5pt" type="#_x0000_t75"><img id="ViewPicture.ascx_GalleryImage" style="BORDER-RIGHT: black 2px solid; BORDER-TOP: black 2px solid; BORDER-LEFT: black 2px solid; WIDTH: 640px; BORDER-BOTTOM: black 2px solid; HEIGHT: 260px" alt="图2" src="http://p.blog.csdn.net/images/p_blog_csdn_net/summerfang/208493/r_2.PNG"><font size="3"></font></shape>

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.

<shape id="_x0000_i1027" style="WIDTH: 697.5pt; HEIGHT: 287.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CDOCUME~1%5CSUMMER~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

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.

你可能感兴趣的:(.net,PHP,ext,单元测试,F#)