php单元测试手册,PHP单元测试使用手册

1单元测试简介

1.1单元测试

单元测试:是指对软件中的最小可测试单元进行检查和验证。(单一模块、一个过程、一个函数等)

1.2单元测试范围和目标

单元测试包含计划阶段、设计阶段、实现阶段和执行阶段。起始于详细设计,主要是各模块的源代码进行测试,直到单元阶段结束后终止。此时主要是PHP单元测试。

单元测试的目标是隔离程序部件并证明这些单个部件是正确的。一个单元测试提供了代码片断需要满足的严密的书面规约。

2安装与使用

2.1安装PHPUnit

2、添加执行权限 chmod +x phpunit.phar

3、更改路径mv phpunit.phar  /usr/local/bin/phpunit

4、查看phpunit版本 phpunit --version

2.2使用

1、如有详细设计文档,先仔细阅读详细设计文档;针对需要测试的PHP代码先进行静态走读(查看是否正规编写、查看是否有逻辑错误、查看是否有未实现的功能等。)

2、编写测试脚本(以xxx.php为例)

1)首先是调用xxx配置文件以及调用PHPunit

b6914aad98d8ea417fe7795100e0ea94.png有些是这样的:

/**************测试加载******************/

const CONFIG_ENV = 'office';

// include_once __DIR__."/../../sdk/Autoload.php";

// include_once "ApiHelper.php";

include('/data/wuheyou_php/trunk/sdk/Autoload.php');

ImportModule('home');

2)然后测试模块加载类

c0648218ba09a97eee9058f562467ec8.png

3)最后编写测试用例(根据php代码,分析等,传入正确参数、错误参数、缺失参数、多余参数、路径覆盖、条件判断等)

276ff395b9de40f765bcb2d6656f1ce8.png访问私有函数

public function testuserTechDonateResource(){

print("资源争夺战个人科技贡献");

for ($i=0; $i<=20; $i++){

//资源争夺战 8基础战力,9幸运倍率,10守城加成,11宽度

$boss = new GangTech(144540,136);

$method = new ReflectionMethod('GangTech','userTechDonateResource');

$method->setAccessible(true);

$tech_id = 9;

$type = 3;//资源争夺战

$result = $method->invoke($boss,$tech_id,$type);

var_dump($result);

$this->assertArraySubset(["result"=>"true"],array("result"=>"true","code"=>0,"msg"=>"成功","data"=>array("next_score"=>60,"star"=>3)));

}

}例子:1

public function testPublishGuessodds(){

echo "odds为负数!\n";

$guess = new GameGuess();

$uid = 186;

$guessdesc = "这是我的猜猜猜22222";

//$options = array(array('option_name'=>'我是谁', 'odds'=>1.6),array('option_name'=>'我是谁2', 'odds'=>1.5));//传入多组

$options[] = array('option_name'=>'是是是是是', 'odds'=>-1.6);//传入单组

$aborttime = '1449049541';

$result = $guess->publishGuess($uid,$guessdesc,$options,$aborttime);

// var_dump($result);

$this->assertEquals('赔率设置不合法',$result->msg);

}

例子2:

public function testbestlist(){

print ("\n 城市转转转押注情况列表\n");

$list = new GameCitytrun();

$data = array('uid' =>183 , 'gtypeid' =>20 );

$result = $list->betList($data);

var_dump($result);

$this->assertTrue(true);

}

例子3:

public function testdoSpin(){

print ("摇奖生成随机数,作为×××的布局\n");

$DSp = new GameBetTiger();

$method = new ReflectionMethod('GameBetTiger','doSpin');

$method->setAccessible(true);

$res_num = array();

$this->res_num = array(

"1" => Array

(

"11" => 4,

"21" => 2,

"31" => 5

),

"2" => Array

(

"12" => 5,

"22" => 2,

"32" => 7

),

"3" => Array

(

"13" => 4,

"23" => 2,

"33" => 4

),

"4" => Array

(

"14" => 2,

"24" => 2,

"34" => 9

),

"5" => Array

(

"15" => 2,

"25" => 5,

"35" => 8

)

);

$result = $method->invoke($DSp);

var_dump($this->res_num);

3、执行对比返回结果 phpunitxxx.php

1)正确数据发送请求时,查看返回数据是否正确、是否会报错、数据库中是否正常插入数据、关闭相关数据库是否能正常工作等。

2)错误数据发送请求时,查看返回数据是否有异常处理、是否有报错、数据是否有异常、等。

3)发现BUG,在提交BUG指向相关开发并督促修复bug,只至关闭BUG。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值