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

标签: 测试classfunctionreport单元测试testing
8465人阅读 评论(6) 收藏 举报
分类:

 

第十章 代码覆盖率分析

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

 

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.

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:184600次
    • 积分:2100
    • 等级:
    • 排名:第18164名
    • 原创:13篇
    • 转载:5篇
    • 译文:13篇
    • 评论:41条
    最新评论