This time to everyone to bring PHPUnit Interface automation test function implementation, PHPUNIT interface Automation test function to realize the considerations of what, the following is the actual case, take a look.
An accidental opportunity at the beginning of the year came into contact with PHPUnit, an open source software developed in the PHP programming language, and a unit testing framework, which can greatly improve the efficiency of interface traversal. Nonsense not much to say, direct dry.
1. Installation
In the Directory of PHP
Pear Channel-discover pear; Pear Install Phpunit/phpunit
2. Configuration
First create a new Lib folder to store the configuration file, and then create a new transfer.php file
<?phpfunction do_post ($url, $fields, $extraheader = Array ()) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_postfields, $fields); curl_setopt ($ch, Curlopt_httpheader, $extraheader); curl_setopt ($ch, Curlopt_returntransfer, true); Get data return $output = Curl_exec ($ch); Curl_close ($ch); return $output;} function Do_get ($url, $extraheader = Array ()) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_httpheader, $extraheader); curl_setopt ($ch, Curlopt_returntransfer, true); Get Data return://curl_setopt ($ch, Curlopt_verbose, true); $output = curl_exec ($ch); Curl_close ($ch); return $output;} function Do_put ($url, $fields, $extraheader = Array ()) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_customrequest, ' PUT '); curl_setopt ($ch, Curlopt_postfields, $fields); curl_setopt ($ch, Curlopt_httpheader, $extraheader); curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back//curl_setopt ($ch, curlopt_encoding, '); $output = curl_exec ($ch); Curl_close ($ch); return $output;} function Do_delete ($url, $fields, $extraheader = Array ()) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_customrequest, ' DELETE '); curl_setopt ($ch, Curlopt_postfields, $fields); curl_setopt ($ch, Curlopt_httpheader, $extraheader); curl_setopt ($ch, Curlopt_returntransfer, true); Get Data back//curl_setopt ($ch, curlopt_encoding, '); $output = curl_exec ($ch); Curl_close ($ch); return $output;}
Finally, create a new basetest.php file
<?php require_once ("transfer.php"); Define ("PREFIX", "http://xxx"); Define ("Httpsprefix", "https://xxx"); function Build_get_param ($param) { return http_build_query ($param);}
To this interface the test environment is built.
3. Writing test Cases
<?php$basedir = DirName (FILE); require_once ($basedir. '/lib/basetestdev.php ');d efine ("PHONE", "xxx");d efine ("PWD", "xxx");d efine ("PostURL", "xxx"); class Testapi extends Phpunit_framework_testcase {Private Function call_http ($path, $param, $expect = ' OK ') {$_param = Build_get_par AM ($param); $url = PREFIX. "$path?". $_param; $buf = Do_get ($url); $obj = Json_decode ($buf, True); $this->assertequals ($obj [' retval '], $expect); return $obj; } Private Function Call_https ($path, $param, $expect = ' OK ') {$_param = Build_get_param ($param); $url = Httpsprefix. "$path?". $_param; $buf = Do_get ($url); $obj = Json_decode ($buf, True); $this->assertequals ($obj [' retval '], $expect); return $obj; } public Function Testlogin () {$param = array (' type ' = ' = ' phone ', ' token ' = + phone, ' password ' =& Gt PWD); $url = ' login '; return $this->call_http ($url, $param); }/** * @depends Testlogin */Public Function TestInfo (array $user) {$session = $user [' retinfo '] [' Session ']; $param = Array (' session ' = = $session); $url = ' info '; return $this->call_http ($url, $param); }
If the POST request
Public Function Testpost () { $session = $user [' retinfo '] [' SessionID ']; $param = Array ( , ' data ' = ' 111 ' ); $url = PostURL. ' PostURL '; Return Do_post ($url, $param); }
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
thinkphp Implementation Payment (JSAPI payment) process Tutorial _php instance
Request a refund from PHP