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.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值