基于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("[email protected]","123456","[email protected]");

            $this->assertEquals("ok", $a1->result);

        }

        */



        public function testLogin()

        {

            $a = login("[email protected]","123456");

            echo $a->result."<br />";

            echo $a->message."<br />";

            echo $a->token."<br />";

        }





        public function testApply()

        {

            $a = login("[email protected]","123456");

            echo $a->result."<br />";

            $a1 = apply($a->token,4277);

            echo $a1->result."<br />";

            echo "message is :".$a1->message."<br />";

        }



    }



?>

 

$phpunit test.php

你可能感兴趣的:(PHP)