1. 数据供给器
用来提供参数和结果,使用 @dataprovider
标注来指定使用哪个数据供给器方法。例如检测app升级数据是否符合预期,addproviderappupdatedata()提供测试的参数和结果。testappupdatedata()检测appupdatedata()返回的结果是否和给定的预期结果相等,即如果$appid='apple_3.3.2_117'
, $result=['status' => 0, 'isios' => fal]
, 则$data中如果含有['status' => 0, 'isios' => fal]
, 则断言成功。建议在数据提供器,逐个用字符串键名对其命名,这样在断言失败的时候将输出失败的名称,更容易定位问题
。
示例代码:
<?php namespace tests\unit; u app\rvices\clientrvice; u 自我陈述报告高中800字tests\testca; class clientrvicetest extends testca { /** * @dataprovider addproviderappupdatedata * * @param $appid * @param $result */ public function testappupdatedata($appid, $result) { $data = (new clientrvice($appid))->appupdatedata(); $this->asrttrue(count(array_interct_assoc($data, $result)) == count($result)); } public function addproviderappupdatedata() { return [ 'null' => [null, ['status' => 0, 'isios' => fal, 'latest_version' => 'v']], 'error app id' => ['sdas123123', ['status' => 0, 'isios' => fal, 'latest_version' => 'v']], 'android force update' => ['bx7_3.3.5_120', ['status' => 0, 'isios' => fal]], 'ios force update' => ['apple_3.3.2_117', ['status' => 1, 'isios' => true]], 'android soft update' => ['sanxing_3.3.2_117', ['status' => 2, 'isios' => fal]], 'ios soft update' => ['apple_3.3.3_118', ['status' => 2, 'isios' => true]], 'android normal' => ['fhqd_3.3.6_121', ['status' => 1, 'isios' => fal]], 'ios normal' => ['apple_3.3.5_120', ['status' => 1, 'isios' => true]], 'h5' => ['h5_3.3.3', ['status' => 1, 'isios' => fal]] ]; } }
断言成功结果:
2. 断言方法
常用有asrttrue(), asrtfal(), asrtnull(), asrtequals(), asrtthat()。
asrtthat()自定义断言。常用的约束有isnull()、istrue()、isfal()、isinstanceof();常用的组合约束logicalor()、logicaland()
。例如检测返回的结果是否是null或apiapp类。
示例代码:
<?php namespace tests\unit; u app\models\apiapp; u app\rvices\systemconfigrvice; u tests\testca; class systemconfigrvicetest extends testca { /** * @dataprovider additionprovidergetlatestupdateappapi * * @param $apptype */ public function testgetlatestupdateappapi($apptype) { $result = systemconfigrvice::getlatestupdateappapi($apptype); $this->asrtthat($result, $this->logicalor($this->isnull(), $this->isinstanceof(apiapp::class))); } public function additionprovidergetlatestupdateappapi() { return [ 'apple' => [1], 'android' => [2], 'null' => [9999] ]; } }
断言成功结果:
3. 对异常进行测试
使用expectexceptioncode()
对错误码进行检测,不建议对错误信息文案进行检测。例如检测设备被锁后是否抛出3026错误码。
示例代码:
<?php namespace tests\unit; u app\rvices\urcurityrvice; u illuminate\support\facades\cache; u tests\testca; class urcurityrvicetest extends testca { public static $urid = 4; /** * 设备锁检测 * @throws \app\exceptions\urexception */ public function testdevicechecklock() { $this->expectexceptioncode(3026); cache::put('device-login-error-account-', '1,2,3,4,5', 300); urcurityrvice::$request = null; urcurityrvice::$udid = null; urcurityrvice::devicecheck(lf::$urid); } }
断言成功结果:
4. 测试私有属性和私有方法使用反射机制
如果只测试私有方法可使用reflectionmethod()
反射方法,使用taccessible(true)
设置方法可访问,并使用invokeargs()或invoke()
调用方法(invokeargs将参数作为数组传递)。例如检测ip是否在白名单中。
示例代码:
被检测代码:
namespace app\facades\rvices; /** * class webdefender */ class webdefenderrvice extends barvice { //ip白名单 private $ipwhitelist = [ '10.*', '172.18.*', '127.0.0.1' ]; /** * ip是否在白名单中 * * @param string $ip * * @return bool */ private function checkipwhitelist($ip) { if (!$this->ipwhiteatp是什么list || !is_array($this->ipwhitelist)) { return fal; } foreach ($this->ipwhitelist as $item) { if (preg_match("/{$item}/", $ip)) { return true; } } return fal; } }
检测方法:
<?php namespace tests\unit; u app\facades\rvices\webdefenderrvice; u tests\testca; class webdefendertest extends testca { /** * 测试ip白名单 * @dataprovider additionproviderip * * @param $ip * @param $result * * @throws \reflectionexception */ public 死性不改歌词function testipwhite($ip, $result) { $checkipwhitelist = new \reflectionmethod(webdefenderrvice::class, 'checkipwhitelist'); $checkipwhitelist->taccessible(true); $this->asrtequals($result, $checkipwhitelist->invokeargs(new webdefenderrvice(), [$ip])); } public function additionproviderip() { return [ '10 ip' => ['10.1.1.7', true], '172 ip' => ['172.18.2.5', true], '127 ip' => ['127.0.0.1', true], '192 ip' => ['192.168.0.1', fal] ]; } }
测试私有属性可使用reflectionclass()
, 获取属性用getproperty()
, 设置属性的值用tvalue()
, 获取方法用getmethod()
, 设置属性和方法可被访问使用taccessible(true)
。例如检测白名单路径。
示例代码:
被检测代码:
<?php namespace app\facades\rvices; u app\exceptions\exceptioncode; u app\exceptions\urexception; u illumin高一生物必修一复习提纲ate\support\facades\cache; /** * cc攻击防御器 * class webdefender */ class webdefenderrvice extends barvice { //路径白名单(正则) private $pathwhitelist = [ //'^auth\/(.*)', ]; private static $request = null; /** * 请求路径是否在白名单中 * * @return bool */ private function checkpathwhitelist() { $path = ltrim(lf::$request->getpathinfo(), '/'); if (!$path || !$this->pathwhitelist || !is_array($this->pathwhitelist)) { return fal; } foreach ($this->pathwhitelist as $item) { if (preg_match("/$item/", $path)) { return true; } } return fal; } }
检测方法:
<?php namespace tests\unit; u app\facades\rvices\webdefenderrvice; u illuminate\http\request; u tests\testca; class webdefendertest extends testca { /** * 检测白名单路径 * @dataprovider additionproviderpathwhitelist * * @param $pathproperty * @param $request * @param $result * * @throws \reflectionexception */ public function testcheckpathwhitelist($pathproperty, $request, $result) { $reflectedclass = new \reflectionclass('app\facades\rvices\webdefenderrvice'); $webdefenderrvice = new webdefenderrvice(); $reflectedpathwhitelist = $reflectedclass->getproperty('pathwhitelist'); $reflectedpathwhitelist->taccessible(true); $reflectedpathwhitelist->tvalue($webdefenderrvice, $pathproperty); $reflectedrequest = $reflectedclass->getproperty('request'); $reflectedrequest->taccessible(true); $reflectedrequest->tvalue($request); $reflectedmethod = $reflectedclass->getmethod('checkpathwhitelist'); $reflectedmethod->taccessible(true); $this->asrtequals($result, $reflectedmethod->invoke($webdefenderrvice)); } public function additionproviderpathwhitelist() { $allpath = ['.*']; $checkpath = ['^auth\/(.*)']; $authndsmsrequest = new request([], [], [], [], [], ['http_host' => 'api.dev.com', 'request_uri' => '/auth/ndsms']); $indexrequest = new request([], [], [], [], [], ['http_host' => 'api.dev.com', 'request_uri' => '/']); $nomatchrequest = new request([], [], [], [], [], ['http_host' => 'api.dev.com', 'request_uri' => '/product/ndsms']); return [ 'index' => [[], $authndsmsrequest, fal], 'no request' => [$allpath, $indexrequest, fal], 'all request' => [$allpath, $authndsmsrequest, true], 'check auth sms' => [$checkpath, $authndsmsrequest, true], 'check path no match' 大会来稿=> [$checkpath, $nomatchrequest, fal] ]; } }
5. 代码覆盖率
使用–coverage-html导出的报告含有类与特质覆盖率、行覆盖率
、函数与方法覆盖率。可查看当前单元测试覆盖的范围。例如输出webdefendertest的代码覆盖率到桌面(phpunit tests/unit/webdefendertest –coverage-html ~/desktop/test)
6. 指定代码覆盖率报告要包含哪些文件
在配置文件(phpunit.xml)里设置whitelist中的processuncoveredfilesfromwhitelist=true, 设置目录用<directory>标签,设置文件用<file>标签。例如指定app/rvices目录下的所有文件和app/facades/rvices/webdefenderrvice.php在报告中。
示例代码:
<?xml version="1.0" encoding="utf-8"?> <phpunit backupglobals="fal" backupstaticattributes="fal" bootstrap="tests/bootstrap.php" colors="true" converterrorstoexceptions="true" convertnoticestoexceptions="true" convertwarningstoexceptions="true" processisolation="fal" stoponfailure="fal"> <testsuites> <testsuite name="unit"> <directory suffix="test.php">./tests/unit</directory> </testsuite> <testsuite name="feature"> <directory suffix="test.php">./tests/feature</directory> </testsuite> </testsuites> <filter> <whitelist processuncoveredfilesfromwhitelist="true"> <directory suffix=".php">./app/rvices</directory> <file>./app/facades/rvices/webdefenderrvice.php</file> </whitelist> </filter> <php> <rver name="app_env" value="local"/> <rver name="bcrypt_rounds" value="4"/> <rver name="cache_driver" value="credis"/> <rver name="mail_driver" value="array"/> <rver name="queue_connection" value="sync"/> <rver name="ssion_driver" value="array"/> <rver name="app_config_cache" value="bootstrap/cache/config.phpunit.php"/> <rver name="app_rvices_cache" value="bootstrap/cache/rvices.phpunit.php"/> <rver name="app_packages_cache" value="bootstrap/cache/packages.phpunit.php"/> <rver name="app_routes_cache" value="bootstrap/cache/routes.phpunit.php"/> <rver name="app_events_cache" value="bootstrap/cache/events.phpunit.php"/> </php> </phpunit>
7. 参考文档
phpunit官方文档 https://phpunit.readthedocs.io/zh_cn/latest/index.html
反射类
反射方法
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-08 05:56:53,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a1b084489e9ad3270e6e0bb62bbbb88b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHPUnit + Laravel单元测试常用技能.doc
本文 PDF 下载地址:PHPUnit + Laravel单元测试常用技能.pdf
留言与评论(共有 0 条评论) |