三默的专栏 - SaaS,敏捷开发方法,最佳实践

单元测试 自动测试 测试驱动开发

用户操作
[即时聊天] [发私信] [加为好友]
方剑斌ID:summerfang
88195次访问,排名1073好友0人,关注者0
summerfang的文章
原创 12 篇
翻译 13 篇
转载 5 篇
评论 31 篇
三默的公告
>>喜欢我的文章就收藏<<
我喜欢的照片软件:
最近评论
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
carry1002:你好,我是猎头公司carry,我们服务的对象主要是世界500强企业,现在有thougthtworks公司的职位机会,TW是敏捷方法领域的领头羊,有兴趣的朋友请和我联系,我的msn:carry.1@hotmail.com
weblogic2009:想象而已. (开源的可视化自定义web表单工具,在: http://my5155.meibu.com )
weblogic2009:想象而已. (开源的可视化自定义web表单工具,在: http://my5155.meibu.com )
nethermit:什么阶段用什么方法,切忌拔苗助长,也忌过度施肥
文章分类
收藏
    相册
    PHPUnit
    随笔
    友情链接
    [1]草莓佳佳和苹果妈妈
    [2]hanshan的个人门户
    [3]软件测试和质量专栏
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    翻译 PHPUnit袖珍指南 第十章 代码覆盖率分析收藏

    新一篇: 中国首届敏捷大会纪行 | 旧一篇: CSDN博客工具配置部分存在Bug,导致不能加载Google搜索

     

    第十章 代码覆盖率分析

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

     

    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.

     

    发表于 @ 2006年05月31日 21:05:00|评论(loading...)|编辑

    新一篇: 中国首届敏捷大会纪行 | 旧一篇: CSDN博客工具配置部分存在Bug,导致不能加载Google搜索

    评论

    #ralf 发表于2006-06-27 16:59:00  IP: 221.222.21.*
    翻译完了吗?好像还有PHP4的版本没翻译阿,期待中
    #summerfang 发表于2006-06-28 01:38:00  IP: 60.166.140.*
    抱歉,世界杯,比较忙。:)
    #aaron 发表于2006-07-27 11:55:00  IP: 210.21.224.*
    请教一个问题:
    测试代码从PHPUnit1基础升级到PHPUnit2基础,是否容易实现?
    现在PHP4的代码,并且暂时不会升级到PHP5,是否有必要在升级到PHP5之前,用PHPUnit1为基础实现测试代码的编写?
    #summerfang 发表于2006-07-27 12:02:00  IP: 61.191.27.*
    PHPUnit2主要针对PHP5的。PHP5提供了更好的面向对象的特性,所以PHPUnit1不是很适用了。

    这种迁移总是比较痛苦的。
    #aaron 发表于2006-07-27 13:21:00  IP: 210.21.224.*
    我的意思是:
    在PHP4系统下用PHPUnit1为基础编写的测试代码。当系统升级到PHP5和PHPUnit2之后,能否对原来测试代码作少量改动就可以在新的系统下应用呢?好像没找到官方对这个需求的描述。:-(
    #summerfang 发表于2006-08-01 13:41:00  IP: 61.191.27.*
    我想不行。

    PHPUnit,包括所有的XUnit,最核心的是测试用例。这一点PHPUnit1和PHPUnit2没有质的区别。

    从PHPUnit1到2,主要是框架实现类的代码迁移,基本上只能用手工。但是,测试用例的实质没有变。
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 三默