测试类
来源:互联网 发布:歼20 歼31 知乎 编辑:程序博客网 时间:2024/04/24 22:10
作者: 溪水清澈
<?php/** * 测试类基类 * 继承时, 参考assertEqual写 */class TestUnitBase{ /** * 测试的返回结果, 格式如下: * array( * 'TestA' => array( * 'info' => TestA方法的相关信息, 参见debug_backtrace()的返回值, * 'methods' => array( * 0 => array( * 'pass' => bool 测试通过与否 * 'desc' => 第三个参数 * 'info' => assertXXX方法的相关信息 * ), * 1 => ... * ) * ), * ); */ public $testResult; /** * 具体的值与期望值判断, 并对结果进行赋值 */ final public function assertEqual( $value, $expectedValue = true, $desc = '' ) { $pass = $value == $expectedValue; $this -> assignResult( $pass, $desc ); } /** * 对结果进行赋值 * 每执行一个assertXXX, * 把这个assertXXX的测试结果放到以testXXX为键的结果数组中. */ final private function assignResult( $result, $desc ) { $tm = $this -> getTestMethod(); $am = $this -> getAssertMethod(); $testKey = $tm[ 'function' ]; if( empty( $this -> testResult[ $testKey ][ 'info' ] )) { $this -> testResult[ $testKey ][ 'info' ] = $tm; } $this -> testResult[ $testKey ][ 'methods' ][] = array( 'pass' => $result, 'desc' => $desc, 'info' => $am ); } /** * 取得子类中的测试方法的信息 */ final private function getTestMethod() { // 索引3为结果 $backtrace = debug_backtrace(); return $backtrace[ 3 ]; } /** * 取得assertXXX方法的信息 */ final private function getAssertMethod() { // 索引2为结果 $backtrace = debug_backtrace(); return $backtrace[ 2 ]; }}/** * 测试管理类, 以类名为参数 * 自动执行testXXX函数 */class TestManage{ private $testClassName; private $testObject; /** * 以类名为参数 */ public function __construct( $testClassName ) { $this -> testClassName = $testClassName; } /** * 执行测试 */ public function exec() { if( ! $this -> newObject() ) { return false; } $testMethods = $this -> getAllTestMethods(); foreach( $testMethods as $method ) { call_user_func( array( $this -> testObject, $method -> name )); } $this -> result = $this -> testObject -> testResult; return $this -> result; } /** * 实例化对象 */ private function newObject() { if( class_exists( $this -> testClassName )) { $this -> testObject = new $this -> testClassName; return true; } else { return false; } } /** * 取得测试类的测试方法 * 暂定为以test开头的公共方法(不论大小写); */ private function getAllTestMethods() { $ref = new ReflectionClass( $this -> testObject ); $methods = $ref -> getMethods( ReflectionMethod::IS_PUBLIC ); foreach( $methods as $method ) { if( 'test' == strtolower( substr( $method -> name, 0, 4 )) && $method -> class == $this -> testClassName ) { $ret[] = $method; } } return $ret; } /** * 输出报告 */ public function report() { foreach( $this -> result as $res ) { $class = $res[ 'info' ][ 'class' ]; $method = $res[ 'info' ][ 'function' ]; $type = $res[ 'info' ][ 'type' ]; echo "$class$type$method ## \n"; foreach( $res[ 'methods' ] as $assert ) { $pass = $assert[ 'pass' ] ? 'pass' : "\terror"; $desc = ( $assert[ 'pass' ] ? "\t" : '' ) . $assert[ 'desc' ]; $assertMethod = $assert[ 'info' ][ 'function' ]; $assertLine = $assert[ 'info' ][ 'line' ]; echo "\t$assertMethod @ $assertLine\t$pass\t$desc\n"; } echo "\n"; } }}// 以下为测试代码class TestTest extends TestUnitBase{ public function testA() { $this -> assertEqual( 1, 2, '1 eq 2?' ); $this -> assertEqual( 1, 1, '1 eq 1?' ); $this -> assertEqual( 1, 2, '1 eq 2?' ); } public function testB() { $this -> assertEqual( 'abc', 'def', 'eq?' ); }}$test = new TestManage( 'TestTest' );$test -> exec();$test -> report();
- 测试类
- 测试类
- 测试类
- TestNG指南5-测试方法、测试类与测试组
- Juint测试类、测试集、测试运行器
- 黑盒测试(二)-----等价类测试
- Android测试(1)-普通类的测试
- junit4同时测试多个测试类
- Andriod测试类和测试内容
- 使用httpservlet测试类测试interface
- 自制性能测试类
- 软件测试类内容
- 自己测试的类
- 测试验证码类
- spring的测试类
- TestTri 三角形测试类
- 测试类网站
- JUnit4的测试类
- 信息检索领域书籍推荐
- ESXi5.x上安装dell OpenManage 6.5 / 7.0
- OpenGl 的基本绘图功能
- 一个linux UDP网络通讯的例子源代码(server、client方式)
- sp_executesql介绍和使用
- 测试类
- Java安全(加密、摘要、签名、证书、SSL、HTTPS)
- Android开的资源相关
- NYOJ 斐波那契数列四吧 南工314
- C文件操作
- 精心挑选的12款优秀 jQuery 手风琴效果插件和教程
- Android 滑动效果入门篇(一)—— ViewFlipper
- ubuntu12.04使用---在左侧桌面添加图标
- 改变窗体形状