首先需要下载SimpleTest 的包:https://github.com/simpletest/simpletest 或官网下载 http://simpletest.org/
我们会看到这样的文件目录结构
我们新增loading.php文件
代码如下:
require_once( 'autorun.php'); require_once( 'url.php'); require_once( 'form.php'); require_once( 'page.php'); require_once( 'encoding.php'); require_once('web_tester.php'); require_once('my_public.php'); Mock::generate('SimplePage'); define('urlHostHttp', 'http://'); define('urlHost', 'www.xxxx.com'); /** * 地址 */ define('c_url', urlHostHttp . urlHost); 新增my_public.php 代码如下: class cPublic { public static function postData() { } public static function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = '') { $tmpInfo = ''; $cookiepath = getcwd() . './' . $cookiejar; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); if ($referer) { curl_setopt($curl, CURLOPT_REFERER, $referer); } else { curl_setopt($curl, CURLOPT_AUTOREFERER, 1); } if ($post) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); } if ($cookie) { curl_setopt($curl, CURLOPT_COOKIE, $cookie); } if ($cookiejar) { curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath); } //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 100); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $tmpInfo = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); return $tmpInfo; } }
单元测试类: tool_test.php
代码如下:
require_once( './simpletest/loading.php'); /** * 登录-注册相关测试 * @author chenran 2017-3-13 */ class TestLogin extends UnitTestCase { /** * 用户登录相关测试 * @author chenran 2017-3-13 */ function testUserLogin() { $result = cPublic::vcurl(c_url . '/home/index.php?act=login&op=index', ['user_name' => 'xxx', 'password' => 'xxx', 'code' => '121212']); $result = json_decode($result, true); $this->assertEqual(1, $result['code'], '用户登录测试'); } /** * 判断用户名是否可以注册 * @author chenran 2017-3-13 */ function testIsRegister() { $result = cPublic::vcurl(c_url . '/home/index.php?act=account&op=userNameIs&user_name=xxx'); $result = json_decode($result, true); $this->assertEqual(1, $result['code'], '判断用户名是否可以注册'); $result = cPublic::vcurl(c_url . '/home/index.php?act=account&op=userNameIs&user_name=xxx' . rand(1000, 9999)); $result = json_decode($result, true); $this->assertEqual(1, $result['code'], '判断用户名是否可以注册'); } }
然后只需要在命令行执行php tool_test.php
就能看到单元测试结果了。
关键字词: