PHPUnit API:
PHPUnit Manual - PHPUnit latest Manual
其实很简单,
只要你一个类继承了TestCase
,然后这个类有一个public function testXXXXX(){ }
格式的方法,这个方法就是一个测试用例了。注意:必须是public 修饰的方法,除了用test
作为测试方法名称的前缀外,还可以在方法的文档注释块中用@test
标注来将其标记为测试方法。
/**
* @test
*/
public function abc0()
{
$this->assertEquals(0, $this->ba->getBalance());
}
在该方法里写要测试的代码,要怎么测试,就看你自己的了
最后的结果调用$this->assertXXXXXX()
方法,通常是$this->assertEquals()
判断测试值与预期值。
<?php
require __DIR__ . '/../TestCase.php';
use Request;
use appcontrollerUser;
class UserTest extends TestCase
{
/**
* 用户登陆测试用例
*/
public function testLogin()
{
$_GET['account'] = 15522634982;
$_GET['code'] = 666666;
$fans = new User();
$request = Request::instance();
$res = $fans->simpleLogin($request);
// 比如code 200 是正常
$this->assertEquals(200, $res['code']);
}
}
php的单元测试:phpunit 在其官网有详细教程,这里介绍一些补充内容。 首先要明白phpunit软件本身就是由php语言实现,其使用是通过命令行进行的,并不是通过浏览器访问 明白这一点很重要,许多新人被卡在这里。很基础也很简单,但似乎没怎么被提及。
原文:https://zhuanlan.zhihu.com/p/45108980
例子如下:
<?php use SConfig; use ServiceriskDispatch; class NoticeTest extends BaseTestTestCase { protected static $map = array(); public function setUp() { parent::setUp(); self::$map = array( Dispatch::REFUSE => "信审拒绝", Dispatch::PASS => "信审通过", Dispatch::PICS => "需重新上传照片", Dispatch::PHONE => "需通话详单", Dispatch::CANCEL => "订单撤销", Dispatch::UPDATE => "需更新用户数据", Dispatch::CONTRACT => "电子合同签署成功回调" ); } /** * @dataProvider dataProvider 这个的意思就是这个方法的参数 数据提供器是dataProvider ,格式是@dataProvider 数据提供器方法名 */ public function testAction($apply_id, $result, $reason, $credit, $data) { $params = array( 'applyId' => $apply_id, 'reason' => $reason, 'result' => $result, 'creditLine' => $credit, 'data' => $data ); $params = array_merge($params, $this->sign($params)); $response = $this->execRequest('POST', 'api_callback_zrisk_notice', $params); $expect = [ 'retcode' => 2000000, 'msg' => '成功', 'success' => true, ]; $this->assertEquals($expect, $response, self::$map[$result]); } public function dataProvider() { return array( // [applyId, result, reason, credit, data] // 信审拒绝 array('jydhx', 0, "", 10000.00, base64_encode(json_encode(array( "rejectReason" => array( "reason" => "巴拉巴拉巴拉", "reasonForUser" => "资料太厉害,不敢通过" ), )))), // 信审通过 array( 'jydhx1', 1, 'reason', 10000.00, '{"applyInfo":{"appUid":"1","applyId":"jydhx1","businessId":"jydhx","orderType":"LENDER","startTime":"1466416609368"},"assignee":"系统 [system]","autoCheckInfo":{"ageCheck":"1","bankCardCheck":"1","householdCheck":"1","identityCheck":""},"bankcardInfo":{"account":"范佳杭","cardNo":"6217000010046999127","openBank":"建设银行","phone":"18612241017"},"blackListInfo":{"checkStatus":"3"},"callInfo":{"callListResult":"抓取失败","isRealName":"null","land":"北京联通","noContactInThreeDays":"4","startTime":"2012-10-22 16:00:00"},"loanInfo":{"amount":"10000.00","applyTime":"2016-06-20 17:56:26","bearingMethod":"03","graceRepayment":"0.00","is00":"0","organRepaymentMethod":"01","organRepaymentTerm":"0","packageName":"贷款套餐","rate":"","repaymentMethod":"","term":"5","ungraceRepayment":"2000.00","userRepaymentTerm":"5"},"pageModel":{"autoCheckInfo":"20","bankcardInfo":"25","contactInfo":"10","degreeInfo":"30","historyOrderInfo":"50","loanInfo":"02","personInfo":"40","pictureInfo":"45","productInfo":"05","riskInfo":"15","schoolInfo":"35"},"personInfo":{"address":"北京市朝阳区八里庄莱锦文创园CN09","admissionDate":"2007","age":"28","birthday":"1987-10-04","email":"jiahang.fan@beebank.com","gender":"1","graduateDate":"2011","idNo":"510823198710040053","invitationCode":"","isBlackUser":"0","major":"环境工程","maritalStatus":"1","oriPlace":"四川省剑阁县","phone":"18612241017","qqNo":"","schoolName":"西北民族大学","schoolRecord":"1","username":"范佳杭"},"pictureInfo":{"idCardBackUrl":"/tmp/private-huixing/fenqiman/video/20160602/1234_2ef2cd766d628f4b064667e53b3522bf.png","idCardFaceUrl":"/tmp/private-huixing/fenqiman/video/20160602/1234_d863a3b8942b820fed3eb45148d05090.png","idCardHandeldUrl":"/tmp/private-huixing/fenqiman/video/20160602/1234_4fe51e2f7bb12974609bae3d0c6ae810.png"},"processInfo":{"status":"ACTIVATE"},"processModel":{"modelKey":"jydhx","processName":"教育贷流程","tenantId":"all"},"productInfo":{"coursePeriod":"5","organAddress":"","productName":"天王盖地虎","productOrgan":"","productOrganBranch":"","productPrice":"10000.00"},"ruleSet":{"approvalRule":"346031e724cc4d598c4f885147932567","autoCheckRule":"e74958eb79424a6ba8650959ed23dab4","dataSupplementRule":"5b23a085e7384323a4ea5b68f12526d1","oriPlaceCheckRule":"a0af18440e5e43e397287d42adcf84be"},"serId":"apply_test_edu","startTime":1466416619585,"taskInstId":"2675011"}' ), // 需要重新上传照片 array( "jydhx1", 2, "", "", base64_encode(json_encode(array( "rejectReson" => array( array("reason"=>"原因1", "reasonForUser"=>"友好原因1"), array("reason"=>"原因2", "reasonForUser"=>"友好原因2"), ), ))) ), // 重新抓取通话详单 array('jydhx1', 3, "", "", base64_encode("{}")), // 撤销订单 array('jydhx1', 4, "", "", base64_encode("{}")), // 更新用户数据 array( "jydhx1", 5, "", "", base64_encode(json_encode(array( "personInfo" => array("qqNo" => 10000), "contractInfo" => array( 'data' => array( ["relationship"=>"好友", "name"=>"王五", "phone"=>13323332333] ) ), ))) ), // 授信合同签署成功回调 array("jydhx1", 6, "", "", base64_encode(json_encode(array( "electContractInfo" => array( "guarantorContractId" => "test_demo_credit_contract_no", // 担保人合同编号 ), ))) ), ); } private function sign($data) { $app_secret = Config::confSecurity('api.zrisk_secret.password'); $app_key = Config::confSecurity('api.zrisk_key.password'); $time = time(); ksort($data); return array( "appkey" => $app_key, "t" => $time, "m" => substr(md5($time . $app_secret . implode('', $data)), 0, 12) ); } }