PHPUnit API:php
PHPUnit Manual - PHPUnit latest Manualjson
其實很簡單,
只要你一個類繼承了TestCase
,而後這個類有一個public function testXXXXX(){ }
格式的方法,這個方法就是一個測試用例了。注意:必須是public 修飾的方法,除了用test
做爲測試方法名稱的前綴外,還能夠在方法的文檔註釋塊中用@test
標註來將其標記爲測試方法。api
/**
* @test
*/
public function abc0()
{
$this->assertEquals(0, $this->ba->getBalance());
}
在該方法裏寫要測試的代碼,要怎麼測試,就看你本身的了瀏覽器
最後的結果調用$this->assertXXXXXX()
方法,一般是$this->assertEquals()
判斷測試值與預期值。app
<?php
require __DIR__ . '/../TestCase.php';
use Request;
use app\controller\User;
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語言實現,其使用是經過命令行進行的,並非經過瀏覽器訪問 明白這一點很重要,許多新人被卡在這裏。很基礎也很簡單,但彷佛沒怎麼被說起。ide
原文:https://zhuanlan.zhihu.com/p/45108980單元測試
例子以下:測試
<?php use S\Config; use Service\Zrisk\Dispatch; class NoticeTest extends \Base\Test\TestCase { 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) ); } }