1. 程式人生 > >PHPUnit袖珍指南 第十章 程式碼覆蓋率分析

PHPUnit袖珍指南 第十章 程式碼覆蓋率分析

第十章程式碼覆蓋率分析

你已經學會了怎麼使用單元測試程式碼,但你怎麼測試你的測試呢?你怎麼發現沒被測試的程式碼,換句話說,沒被測試覆蓋的程式碼?怎麼衡量測試的完整性?所有這些問題的答案就是程式碼覆蓋率分析。程式碼覆蓋率分析告訴你當測試進行時,那些產品程式碼執行過了。

PHPUnit的程式碼覆蓋率分析應用了Xdebug[6]擴充套件提供的語句覆蓋率功能。什麼時語句覆蓋率?舉個例子來說,如果有一個方法有100個程式碼行,在測試進行時,只有75行真正運行了,這個方法的語句覆蓋率就是75%。

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

圖1

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已經被測試完全覆蓋了。

圖2

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.