Testing Api using PHPUnit Laravel
首先复制.env.example文件重命名为.env.testing:
然后,执行:
touch test.sqlite
将在 D:/laragon/www/laraveauth 目录下生成一个test.sqlite文件:
然后我们将.env.testing文件数据库配置修改为:
DB_CONNECTION=sqlite DB_DATABASE="D:/laragon/www/laraveauth/test.sqlite"
接下来执行:
php artisan make:test TaskApiTest
文件位于:
接下来,修改这个文件之前,执行一下
php artisan route:list
查看一下api task请求的相关route:
先来实现测试创建Task的方法:
方法命名为比较容易理解的就行;
发送post请求到/api/task;最后代码如下:
public function testCanCreateTask() { $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => 'next friday', ]; $response = $this->post(route('tasks.store'), $formData); $response->assertStatus(201); }
由于phpunit存在于 vendor\bin文件夹下:
所以:控制台执行:
.\vendor\bin\phpunit.bat
结果:
错误解决:
1.env.testing文件中APP_KEY未设置:
拷贝一个.env文件中的即可;
再次执行:
.\vendor\bin\phpunit.bat
结果:
没有头绪,我们需要这个:
$this->withoutExceptionHandling();
再次执行:
.\vendor\bin\phpunit.bat
可以发现出现了更多的错误trace信息:
结果就是 我们执行store方法前未登录:
修改后及执行结果如下:
修正方法:laragon开启pdo_sqlite扩展。【只要php开启sqlite的扩展就可以了】
我们再在类内添加一个trait
use RefreshDatabase;
这个trait会在每次单元测试的时候刷新数据库。【对migration执行一次migrate】
再次执行:
.\vendor\bin\phpunit.bat
测试通过!
接下来我们断言Json
结果失败:
修改一下及结果仍然失败:
再次修改:
测试通过!
其余的测试不写了。
文档看这里:测试指南
TaskApiTest.php文件如下:
<?php namespace Tests\Feature; use App\Task; use App\User; use Carbon\Carbon; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class TaskApiTest extends TestCase { use RefreshDatabase; protected $user; public function setUp(): void { parent::setUp(); $this->user = factory(User::class)->create(); $this->actingAs($this->user, 'api'); } /** * A basic feature test example. * * @return void */ public function testCanCreateTask() { $due = Carbon::parse('next friday'); $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => $due, ]; $this->withoutExceptionHandling(); $response = $this->post(route('tasks.store'), $formData); $response->assertStatus(201) ->assertJson(['data' => $formData]); } public function testCanShowTask() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $response = $this->get(route('tasks.show', $task->id)); $response->assertStatus(200); } public function testUpdateTask() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $due = Carbon::parse('next friday'); $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => $due, ]; $this->withoutExceptionHandling(); $response = $this->put(route('tasks.update', $task->id), $formData); $response->assertStatus(200) ->assertJson(['data' => $formData]); } public function testDelete() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $response = $this->delete(route('tasks.destroy', $task)); $response->assertStatus(200)->assertJson(['message' => 'Success deleted!']); } public function testShowAllTasks() { $tasks = factory(Task::class, 3)->create(); $this->user->tasks()->saveMany($tasks); $response = $this->get(route('tasks.index')); $response->assertStatus(200)->assertJson($tasks->toArray()); } }