1. 数据供给器
用来提供参数和结果,使用 @dataProvider
标注来指定使用哪个数据供给器方法。例如检测app升级数据是否符合预期,addProviderAppUpdateData()提供测试的参数和结果。testAppUpdateData()检测appUpdateData()返回的结果是否和给定的预期结果相等,即如果$appId='apple_3.3.2_117'
, $result=['status' => 0, 'isIOS' => false]
, 则$data中如果含有['status' => 0, 'isIOS' => false]
, 则断言成功。建议在数据提供器,逐个用字符串键名对其命名,这样在断言失败的时候将输出失败的名称,更容易定位问题
。
示例代码:
<"text-align: center">2. 断言方法
常用有assertTrue(), assertFalse(), assertNull(), assertEquals(), assertThat()。
assertThat()自定义断言。常用的约束有isNull()、isTrue()、isFalse()、isInstanceOf();常用的组合约束logicalOr()、logicalAnd()
。例如检测返回的结果是否是null或ApiApp类。示例代码:
<"text-align: center">3. 对异常进行测试
使用
expectExceptionCode()
对错误码进行检测,不建议对错误信息文案进行检测。例如检测设备被锁后是否抛出3026错误码。示例代码:
<"text-align: center">4. 测试私有属性和私有方法使用反射机制
如果只测试私有方法可使用
ReflectionMethod()
反射方法,使用setAccessible(true)
设置方法可访问,并使用invokeArgs()或invoke()
调用方法(invokeArgs将参数作为数组传递)。例如检测IP是否在白名单中。示例代码:
被检测代码:
namespace App\Facades\Services; /** * Class WebDefender */ class WebDefenderService extends BaseService { //ip白名单 private $ipWhiteList = [ '10.*', '172.18.*', '127.0.0.1' ]; /** * ip是否在白名单中 * * @param string $ip * * @return bool */ private function checkIPWhiteList($ip) { if (!$this->ipWhiteList || !is_array($this->ipWhiteList)) { return false; } foreach ($this->ipWhiteList as $item) { if (preg_match("/{$item}/", $ip)) { return true; } } return false; } }检测方法:
<"htmlcode"><"/$item/", $path)) { return true; } } return false; } }检测方法:
<"color: #ff0000">5. 代码覆盖率使用--coverage-html导出的报告含有类与特质覆盖率、
行覆盖率
、函数与方法覆盖率。可查看当前单元测试覆盖的范围。例如输出WebDefenderTest的代码覆盖率到桌面(phpunit tests/unit/WebDefenderTest --coverage-html ~/Desktop/test)6. 指定代码覆盖率报告要包含哪些文件
在配置文件(phpunit.xml)里设置whitelist中的processUncoveredFilesFromWhitelist=true, 设置目录用<directory>标签,设置文件用<file>标签。例如指定app/Services目录下的所有文件和app/Facades/Services/WebDefenderService.php在报告中。
示例代码:
<"1.0" encoding="UTF-8""false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false"> <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/Services</directory> <file>./app/Facades/Services/WebDefenderService.php</file> </whitelist> </filter> <php> <server name="APP_ENV" value="local"/> <server name="BCRYPT_ROUNDS" value="4"/> <server name="CACHE_DRIVER" value="credis"/> <server name="MAIL_DRIVER" value="array"/> <server name="QUEUE_CONNECTION" value="sync"/> <server name="SESSION_DRIVER" value="array"/> <server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/> <server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/> <server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/> <server name="APP_ROUTES_CACHE" value="bootstrap/cache/routes.phpunit.php"/> <server name="APP_EVENTS_CACHE" value="bootstrap/cache/events.phpunit.php"/> </php> </phpunit>7. 参考文档
PHPUnit官方文档 https://phpunit.readthedocs.io/zh_CN/latest/index.html
反射类 https://www.php.net/manual/en/class.reflectionclass.php
反射方法 https://www.php.net/manual/en/class.reflectionmethod.php以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!华山资源网 Design By www.eoogi.com暂无评论...
更新日志
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】