博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Laravel57单元测试phpunit
阅读量:3986 次
发布时间:2019-05-24

本文共 1225 字,大约阅读时间需要 4 分钟。

首先进入Laravel57根目录,因为本机没有安装phpunit

phpunit --version

提示未找到命令

实际上Laravel的命令在vendor/bin下

将 phpunit.bat 复制到根目录,并修改第3行,修改后如下:

SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunitphpunit --versionPHPUnit 7.5.17 by Sebastian Bergmann and contributors.

在Laravel框架中测试文件夹在根目录下的 tests/

tests/	Feature/		ExampleTest.php	Unit/		ExampleTest.php	CreatesApplication.php  可以创建一个app应用	TestCase.php 	基类,其他的测试文件继承这个类phpunit配置文件,使用默认即可phpunit.xml

先清除配置缓存

php artisan config:clear

创建测试类

// 在 Feature 目录下创建一个测试类...php artisan make:test UserTest// 在 Unit 目录下创建一个测试类...php artisan make:test UserTest --unit

执行测试

phpunit tests/Unit/ExampleTest.php

测试用例编写

普通$this->assertTrue(true);请求地址$response = $this->get('/');$response->assertStatus(200);设置回话信息$response = $this->withSession(['foo' => 'bar'])->get('/');测试json api$response = $this->json('POST', '/user', ['name' => 'Sally']);$response->assertStatus(200)->assertJson(['created' => true,]);// 是否存在精确验证JSON匹配$response = $this->json('POST', '/user', ['name' => 'Sally']);$response->assertStatus(200)->assertExactJson(['created' => true,]);自定义HTTP请求public function testApplication(){
$response = $this->call('GET', '/', ['name' => 'Taylor']); $this->assertEquals(200, $response->status());}。。

转载地址:http://rwaui.baihongyu.com/

你可能感兴趣的文章
No.31- POJ1469 二部图最大匹配模版题
查看>>
No.32 - POJ2125 - 关于最小割的割边或割点
查看>>
No.33 - POJ1523 邻接表Tarjan算法 找关节点
查看>>
No.34 - Codeforces777B 田忌赛马 贪心
查看>>
No.35 - POJ3624 -01背包
查看>>
No.36 - POJ1966 图的联通度-枚举T网络流
查看>>
No.37- LeetCode662 - 树的最大宽度
查看>>
No.38 - LeetCode1025 动态规划-简单
查看>>
No.39 -LeetCode121
查看>>
No.40 LeetCode746
查看>>
No.41 - LeetCode70
查看>>
No.42-LeetCode53
查看>>
No.43 - LeetCode198 动态规划
查看>>
No.44-LeetCode303 前缀和
查看>>
No.45 -LeetCode338 二进制有多少个1
查看>>
No.46 - LeetCode877 -动态规划 -首尾取数字最大 - 博弈
查看>>
No.49-LeetCode188 - 动态规划-K次股票最大收益-很难
查看>>
No.47 - LeetCode392
查看>>
No.48-LeetCode42 - 一维蓄水
查看>>
No.50 - leetCode407- 二维蓄水-边界水面上升-很难
查看>>