Testing Api using PHPUnit Laravel

首先复制.env.example文件重命名为.env.testing:

Testing Api using PHPUnit Laravel

Testing Api using PHPUnit Laravel

然后,执行:

touch test.sqlite

Testing Api using PHPUnit Laravel

将在 D:/laragon/www/laraveauth 目录下生成一个test.sqlite文件:

Testing Api using PHPUnit Laravel

然后我们将.env.testing文件数据库配置修改为:

DB_CONNECTION=sqlite
DB_DATABASE="D:/laragon/www/laraveauth/test.sqlite"

Testing Api using PHPUnit Laravel

接下来执行:

php artisan make:test TaskApiTest

Testing Api using PHPUnit Laravel

文件位于:

Testing Api using PHPUnit Laravel

接下来,修改这个文件之前,执行一下

php artisan route:list

查看一下api task请求的相关route:

Testing Api using PHPUnit Laravel

先来实现测试创建Task的方法:

方法命名为比较容易理解的就行;

Testing Api using PHPUnit Laravel

发送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文件夹下:

Testing Api using PHPUnit Laravel

所以:控制台执行:

.\vendor\bin\phpunit.bat

Testing Api using PHPUnit Laravel

结果:

Testing Api using PHPUnit Laravel

错误解决:

1.env.testing文件中APP_KEY未设置:

Testing Api using PHPUnit Laravel

拷贝一个.env文件中的即可;

再次执行:

.\vendor\bin\phpunit.bat

结果:

Testing Api using PHPUnit Laravel

没有头绪,我们需要这个:

$this->withoutExceptionHandling();

Testing Api using PHPUnit Laravel

再次执行:

.\vendor\bin\phpunit.bat

可以发现出现了更多的错误trace信息:

Testing Api using PHPUnit Laravel

结果就是 我们执行store方法前未登录:

修改后及执行结果如下:

Testing Api using PHPUnit Laravel

修正方法:laragon开启pdo_sqlite扩展。【只要php开启sqlite的扩展就可以了】

Testing Api using PHPUnit Laravel

我们再在类内添加一个trait

use RefreshDatabase;

这个trait会在每次单元测试的时候刷新数据库。【对migration执行一次migrate】

再次执行:

.\vendor\bin\phpunit.bat

Testing Api using PHPUnit Laravel

测试通过!

接下来我们断言Json

Testing Api using PHPUnit Laravel

结果失败:

Testing Api using PHPUnit Laravel

修改一下及结果仍然失败:

Testing Api using PHPUnit Laravel

再次修改:

Testing Api using PHPUnit Laravel

测试通过!

其余的测试不写了。

文档看这里:测试指南

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());
    }
}

Testing Api using PHPUnit Laravel

上一篇:selenium firefox 内存 速度优化


下一篇:Win7中开启系统如何自带FTP服务器