phpunit使用(项目中)

来源:互联网 发布:编写c语言程序步骤 编辑:程序博客网 时间:2024/05/18 02:24

1.命令行(cmd)安装pear

2.命令行(cmd)安装phpunit(phpunit2在06年已废弃)

3.开始编写测试(1.继承类,2.test_开头的方法)

use \Logger;//打印logger类
use jianghu\growing\GuajiAction;//1引入类
use jianghu\location\Location;//2引入类
use jianghu\playerAction\PlayerAction;//3引入类
require_once (dirname(__FILE__) . "/../growing/guaji.php");//1引入类路径
require_once (dirname(__FILE__) . "/../location/location.php");//2引入类路径
require_once (dirname(__FILE__) . "/../playerAction/playerAction.php");//2引入类路径


require_once (dirname(__FILE__) . '/../config.php');//引入类
require_once (dirname(__FILE__) . '/../common/utils.php');//引入类
define ('GoodAccess',true);//引入类
require_once (dirname(__FILE__) . '/../action/actionProcess.php');//引入类
Logger::configure(dirname(__FILE__) . '/../log4php.xml');//引入类
Predis\Autoloader::register();//引入类


class Test2 extends PHPUnit_Framework_TestCase//集成
{
const CONFIG_PATH = "./config.txt";//配置文件路径
function setUp() {
print "setup testing environment\n";
}
/**
* 挂机(事件)
*/
function guaji_1() {
$putong = 0;
$jiyu = 0;
$qiyu = 0;
$handler = new GuajiAction("521d6ed190ce8ef00800000a");
$configArr =  $this->configInfo();
$sys_total = $configArr['sys_总数'];//循环总次数
for($i=0; $i<$sys_total; $i++){
//$this->assertTrue( guaji_action() == 3 ); 
$wenziArr = $handler->guaji_action();
$wenzi = $wenziArr['event'];//事件
if($wenzi == "putong"){
$putong += 1;
}else if($wenzi == "jiyu"){
$jiyu += 1;
}else if($wenzi == "qiyu"){
$qiyu += 1;
}
}
// $logger = \Logger::getLogger("jianghu");
// $logger->debug("普通#".$putong);
// $logger->debug("机遇#".$jiyu);
// $logger->debug("奇遇#".$qiyu);
$putong1 = $sys_total*0.50;
$jiyu1 = $sys_total*0.49;
$qiyu1 = $sys_total*0.01;
print "putong= $putong/$putong1 际遇 $jiyu/$jiyu1  奇遇 $qiyu/$qiyu1\n";
$expected = 0.05;
$actual = abs(($putong/(float)$putong1)-1.0);
$this->assertLessThan($expected, $actual, "普通概率错误-puchong!$putong vs $putong1");
$actual = abs(($jiyu/(float)$jiyu1)-1.0);
$this->assertLessThan($expected, $actual, "际遇概率错误-jiyu!$jiyu vs $jiyu1");
$expected = 0.10; // 概率太低,增加容错
$actual = abs(($qiyu/(float)$qiyu1)-1.0);
$this->assertLessThan($expected, $actual, "奇遇概率错误-qiyu!$qiyu vs $qiyu1");
}

/**
* 接镖(押镖)
*/
function yabiao_2() {
$level1 = 0;
$level2 = 0;
$level3 = 0;
$level4 = 0;
$level5 = 0;
$handler = new Location();
$configArr =  $this->configInfo();
$sys_total = $configArr['sys_总数'];//循环总次数
for($i=0; $i<$sys_total; $i++){
$result=$handler->testYaBiao();
$level = $result['level'];//等级
if($level == "一等镖"){
$level1 += 1;
}else if($level == "二等镖"){
$level2 += 1;
}else if($level == "三等镖"){
$level3 += 1;
}else if($level == "四等镖"){
$level4 += 1;
}else if($level == "五等镖"){
$level5 += 1;
}
}
$gailvLevel1 = $sys_total*0.05;
$gailvLevel2 = $sys_total*0.25;
$gailvLevel3 = $sys_total*0.40;
$gailvLevel4 = $sys_total*0.25;
$gailvLevel5 = $sys_total*0.05;
// $logger = \Logger::getLogger("jianghu");
// $logger->debug("一等镖#".$level1." / ".$gailvLevel1);
// $logger->debug("二等镖#".$level2." / ".$gailvLevel2);
// $logger->debug("三等镖#".$level3." / ".$gailvLevel3);
// $logger->debug("四等镖#".$level4." / ".$gailvLevel4);
// $logger->debug("五等镖#".$level5." / ".$gailvLevel5);
// $logger->debug("######");
print "Level1= $level1/$gailvLevel1 Level2 $level2/$gailvLevel2  Level3 $level3/$gailvLevel3 Level4 $level4/$gailvLevel4  Level5 $level5/$gailvLevel5\n";
$expected = 0.05;//一等镖
$actual = abs(($level1/(float)$gailvLevel1)-1.0);//1
$this->assertLessThan($expected, $actual, "level-1-biao# $level1 vs $gailvLevel1");
$actual = abs(($level2/(float)$gailvLevel2)-1.0);//2
$this->assertLessThan($expected, $actual, "level-1-biao# $level2 vs $gailvLevel2");
$actual = abs(($level3/(float)$gailvLevel3)-1.0);//3
$this->assertLessThan($expected, $actual, "level-1-biao# $level3 vs $gailvLevel3");
$actual = abs(($level4/(float)$gailvLevel4)-1.0);//4
$this->assertLessThan($expected, $actual, "level-1-biao# $level4 vs $gailvLevel4");
$actual = abs(($level5/(float)$gailvLevel5)-1.0);//5
$this->assertLessThan($expected, $actual, "level-1-biao# $level5 vs $gailvLevel5");
}

function configInfo() {
$logger = \Logger::getLogger("jianghu");
$path = Test2::CONFIG_PATH;
$fp=fopen($path,"r");//以只读的方式打开文件
while(!(feof($fp)))
{
$text=fgets($fp);//读取文件的一行
if($text){
$arrLine = explode(":",$text);
}
$key = $arrLine[0];//key
$value = $arrLine[1];//value
//组合数据
$configArr[$key] = $value;
}
fclose($fp);
return $configArr;
}
function test_myown() {
print "running a dummy test\n";
}
}

原创粉丝点击