您好,欢迎访问一九零五行业门户网

什么是PHPUnit?在PHP项目中怎么使用?

什么是phpunit?在php项目中怎么使用?下面本篇文章给大家介绍一下在php项目中使用phpunit框架进行单元测试的方法,希望对大家有所帮助!
镜像地址 : phpunit简介以及如何在项目中使用 - 多厘(https://wulicode.com/php/vendor/phpunit-at-project/index.html)
官方文档:https://phpunit.readthedocs.io/zh_cn/latest/
单元测试介绍单元测试是分别对程序的单元(方法, 函数)进行测试,判断执行结果是否符合预期
单元测试能协助开发者什么?
1.确保单元的执行结果
2.尽早发现程序中的错误
3.修改程序, 更加有信心
phpunitphpunit 是 php 程式语言中最常见的单元测试 (unit testing) 框架,phpunit 是参考 xunit 架构利用 php 实作出来。
安装phpunit
# 项目中安装$ composer require --dev phpunit/phpunit# 下载到本地$ wget https://phar.phpunit.de/phpunit-9.phar
使用项目中安装可以在项目中直接运行, 无需太多配置, 将 phar 下载到非项目文件夹, 可以减少项目代码的体积, 我一般采用下载到本地, 然后多个项目公用一个 phpunit.phar, 减少项目体积, 不过需要在 ide中增加更多配置
配置autoload
添加下面的代码到composer.json packagename是项目的名称,src是包含php class文件的文件夹地址,项目根目录下的子文件夹。
... "autoload": { "psr-4": { "packagename\\": "src/", "packagename\\tests": "tests/" } },...
设置phpunit
在phpstorm中 languages & frameworks >php >test frameworks 根据不同的加载方式设置不同的配置
如果是项目额外加载, 则需要把 phpunit 所在的文件夹作为 library 加入项目, 便于代码提示 preferences | languages & frameworks | php , 加入 phpunit 文件夹
编写phpunit测试
针对类 example 的测试写在类 exampletest 中, exampletest 继承自 testcase
对于方法的测试命名为 test* 的公用方法
在测试方法内,类似于 assertequals() 的断言方法用来对实际值和预期值的匹配做出验证
arraytest 用 phpunit 测试数组操作
<?phpuse phpunit\framework\testcase;class arraytest extends testcase{ public function testpushandpop() { $stack = []; $this->assertcount(0, $stack); $stack[] = 'foo'; $this->assertequals('foo', $stack[count($stack) - 1]); $this->assertcount(1, $stack); $this->assertequals('foo', array_pop($stack)); $this->assertcount(0, $stack); }}
在 ide 中运行
点击方法名称右键测试类右键测试文件夹
phpunit 断言
laravel 中除了标准的 phpunit 断言(assertequals(), assertcontains(), assertinstanceof(), …更多断言请看https://phpunit.readthedocs.io/zh_cn/latest/assertions.html?highlight=asserttrue#)之外, 还存在很多允许测试 web 应用的检测项目
assertpageloaded($uri, $message = null)
检测最近的页面是否被加载, 如果不存在 url / message 时候会报错
assertresponseok()
是否页面相应ok
assertreponsestatus($code)
是否响应指定的code
assertviewhas($key, $value = null)
视图中是否存在指定的数据
assertviewhasall($bindings)
视图中是否存在指定的一系列数据
assertviewmissing($key)
指定视图中是否不存在这个数据
assertredirectedto($uri, $with = [])
检测是否重定向到指定的uri
assertredirectedtoroute($name, $parameters = [], $with = [])
是否客户端重定向到指定的路由
assertredirectedtoaction($name, $parameters = [], $with = [])
是否重定向到 action
assertsessionhas($key, $value = null)
session 中是否存在 key/ value
assertsessionhasall($bindings)
session 中是否存在指定的 kv
assertsessionhaserrors($bindings = [])
session 是否存在错误
asserthasoldinput()
session 中是否存在以前的数据
示例 xml 文件
phpunit.xml at 9.x · laravel
https://github.com/laravel/laravel/blob/9.x/phpunit.xml
<?xml version="1.0" encoding="utf-8"?><phpunit xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true"> <testsuites> <testsuite name="unit"> <directory suffix="test.php">./tests/unit</directory> </testsuite> <testsuite name="feature"> <directory suffix="test.php">./tests/feature</directory> </testsuite> </testsuites> <coverage processuncoveredfiles="true"> <include> <directory suffix=".php">./app</directory> </include> </coverage> <php> <env name="app_env" value="testing"/> <env name="bcrypt_rounds" value="4"/> <env name="cache_driver" value="array"/> <!-- <env name="db_connection" value="sqlite"/> --> <!-- <env name="db_database" value=":memory:"/> --> <env name="mail_mailer" value="array"/> <env name="queue_connection" value="sync"/> <env name="session_driver" value="array"/> <env name="telescope_enabled" value="false"/> </php></phpunit>
php 的单元覆盖率安装 xdebug / phpunit
# 安装 xdebug$ pecl install xdebug
启用 xdebug
# php.ini 文件xdebug.mode = coverage
推荐学习:《php视频教程》
以上就是什么是phpunit?在php项目中怎么使用?的详细内容。
其它类似信息

推荐信息