• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

软件测试中测试用例详解

发布: 2010-4-26 09:50 | 作者: 网络转载 | 来源: 领测软件测试网 | 查看: 115次 | 进入软件测试论坛讨论

领测软件测试网

PHP代码
  1. require_once 'functions.php';  
  2.   
  3. class functionsTest extends PHPUnit_Framework_TestCase  
  4. {  
  5.     /** 
  6.      * 确认url()返回预期的字符串 
  7.      */  
  8.     public function testurl()  
  9.     {  
  10.         define('''index.php');  
  11.   
  12.         C('VAR_MODULE''module');  
  13.         C('VAR_ACTION''action');  
  14.         C('VAR_ROUTER''route');  
  15.   
  16.         // 测试通常模式URL  
  17.         $uri = url('Index''Home''', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  18.         $this->assertEquals('index.php?module=Home&action=Index&q=test&msg=OK'$uri);  
  19.   
  20.         // 测试通常模式路由  
  21.         $uri = url('Index''Home''default', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  22.         $this->assertEquals('index.php?route=default&q=test&msg=OK'$uri);  
  23.   
  24.          
  25.         C('DISPATCH_ON', true);  
  26.         C('URL_MODEL', 1);  
  27.         C('PATH_MODEL', 1);  
  28.          
  29.         // 测试普通PATHINFO模式URL  
  30.         $uri = url('Index''Home''', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  31.         $this->assertEquals('index.php/module/Home/action/Index/q/test/msg/OK'$uri);  
  32.   
  33.         // 测试普通PATHINFO模式路由  
  34.         $uri = url('Index''Home''default', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  35.         $this->assertEquals('index.php/route/default/q/test/msg/OK'$uri);  
  36.   
  37.         C('PATH_MODEL', 2);  
  38.         C('PATH_DEPR''/');  
  39.   
  40.         // 测试智能PATHINFO模式URL  
  41.         $uri = url('Index''Home''', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  42.         $this->assertEquals('index.php/Home/Index/q/test/msg/OK'$uri);  
  43.   
  44.         // 测试智能PATHINFO模式路由  
  45.         $uri = url('Index''Home''default', APP_NAME, array('q' => 'test''msg' => 'OK'));  
  46.         $this->assertEquals('index.php/default/q/test/msg/OK'$uri);  
  47.     }  
  48. }  
  49. ?>  

异常测试

有时程序执行了非法操作而抛出异常,我们需要模拟某个异常,然后捕捉它是否触发了该异常。

PHP代码
  1. class UnitTest extends PHPUnit_Framework_TestCase  
  2. {  
  3.     /** 
  4.      * 测试异常 
  5.      */  
  6.     public function testException()  
  7.     {  
  8.         // 期望Exception异常  
  9.         $this->setExpectedException('Exception');  
  10.   
  11.         // 抛出Exception异常  
  12.         throw new Exception('TestException');  
  13.     }  
  14. }  
  15. ?>  

输出测试

有时某个方法并不返回而输出某些内容,我们需要继承PHPUnit_Extensions_OutputTestCase类来捕捉输出内容。PHPUnit默认不载入扩展类,需要自己加载。

PHP代码
  1. // 载入输出测试用例扩展  
  2. require_once 'PHPUnit/Extensions/OutputTestCase.php';  
  3.   
  4. class UnitTest extends PHPUnit_Extensions_OutputTestCase  
  5. {  
  6.     /** 
  7.      * 测试输出 
  8.      */  
  9.     public function testOutput()  
  10.     {  
  11.         // 期望输出的内容是字符串 'Hello'  
  12.         $this->expectOutputString('Hello');  
  13.   
  14.         // 输出 'Hello'  
  15.         echo 'Hello';  
  16.     }  
  17. }  
  18. ?>  

数据库测试

PHPUnit的数据库测试并不完善,只提供了assertTablesEqual和assertDataSetsEqual两个断言与createFlatXMLDataSet和createXMLDataSet创建XML数据集的方法。无法进行全面的数据操作测试,建议使用DBUnit。

附录

PHPUnit断言参考
assertArrayHasKey($key, array $array, $message = '')
assertArrayNotHasKey($key, array $array, $message = '')
assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '')
assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '')
assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
assertClassHasAttribute($attributeName, $className, $message = '')
assertClassHasStaticAttribute($attributeName, $className, $message = '')
assertClassNotHasAttribute($attributeName, $className, $message = '')
assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
assertContains($needle, $haystack, $message = '')
assertContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode, $checkAttributes = FALSE, $message = '')
assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE)
assertFalse($condition, $message = '')
assertFileEquals($expected, $actual, $message = '', $canonicalizeEol = FALSE)

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

33/3<123

软件测试技术文章排行榜
软件测试技术分类最新内容
关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网