基于PHPUnit编写第一个测试用例

common/config.php 常量模块

<?php

//常量定义

define ("SERVER_ADDR","http://127.0.0.1");

define ("REGISTER_ADDR",SERVER_ADDR."/open/register");

define ("LOGIN_ADDR",SERVER_ADDR."/open/login");

define ("APPLY_ADDR",SERVER_ADDR."/open/apply");

define ("APP_KEY","FEFA4297E4CA6FE8F1E3CCEAB7C1A053");

define ("PROJECT_ID","6360");

?>

 

common/net.php 网络通信模块 要安装curl模块

<?php

function post($url,$fields)
{
    $fields_string = '';
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);
    
    return $result;
}
?>

api.php 被测功能模块

<?php

include_once ("common/net.php");
include_once ("common/config.php");

function login($username,$password)
{
    $url = LOGIN_ADDR;
    $fields = array(
        'username'=>urlencode($username),
        'password'=>urlencode($password),
        'appkey'=>urlencode(APP_KEY),
    );

    $result=json_decode(post($url,$fields));
    return $result;    
}

function register($username,$password,$email)
{
    $url = REGISTER_ADDR;
    
    $fields = array(
        'email'=>urlencode($email),
        'username'=>urlencode($username),
        'password'=>urlencode($password),
        'appkey'=>urlencode(APP_KEY),
        'projectId'=>urlencode(PROJECT_ID),
    );

    $result=json_decode(post($url,$fields));
    return $result;
}

function apply($token,$projectId)
{
    $url = APPLY_ADDR;
    $fields = array(
        'token'=>urlencode($token),
        'projectId'=>urlencode($projectId),
    );

    $result=json_decode(post($url,$fields));
    return $result;
}
?>

 

test.php 自动化测试模块

<?php

    include_once ("api.php");
    require_once('PHPUnit/Framework/TestCase.php');

    class OpenTestCase extends PHPUnit_Framework_TestCase{
        /*
        public function testRegister()
        {
            $a1 = register("test4@qq.com","123456","test4@qq.com");
            $this->assertEquals("ok", $a1->result);
        }
        */

        public function testLogin()
        {
            $a = login("test3@qq.com","123456");
            echo $a->result."<br />";
            echo $a->message."<br />";
            echo $a->token."<br />";
        }


        public function testApply()
        {
            $a = login("test3@qq.com","123456");
            echo $a->result."<br />";
            $a1 = apply($a->token,4277);
            echo $a1->result."<br />";
            echo "message is :".$a1->message."<br />";
        }

    }

?>

 

$phpunit test.php

转载于:https://www.cnblogs.com/code-style/archive/2012/07/03/2575493.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值