eduardoarandah/autohttptests

为Laravel自动生成HTTP测试

2.0.0 2021-03-24 19:56 UTC

This package is auto-updated.

Last update: 2024-08-25 03:59:41 UTC


README

Latest Version on Packagist Software License Total Downloads

不再手动编写测试 =D

只需执行命令,即可将您的操作记录为HTTP测试

php artisan autohttptest:create

imagen

该命令将拦截您的请求,并将响应转换为测试。

完成后,您的测试将保存在tests/Feature/目录中

视频演示

autohttptests-gif

它测试什么?

  • 以相同用户身份进行请求操作
  • 使用相同的动词(GET、PUT、POST)和相同的参数进行请求
  • 断言HTTP响应代码
  • 断言错误
  • 断言重定向

示例代码

<?php

namespace Tests\Feature;
use Tests\TestCase;

class SomethingTest extends TestCase
{
    public function testAutoHttpTest()
    {
        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'         => 'a',
            'lastname'     => 'a',
            'city'         => '',
            'hobbies'   => '',
            'twitter_username' => 'a',
        ])
        ->assertStatus(302)
        ->assertSessionHasErrors([
            'name',
            'country_id',
            'twitter_username',
        ]);

        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'              => 'asdfa',
            'lastname'          => 'asdfa',
            'country_id' => '1',
            'city'              => '',
            'hobbies'        => '',
            'twitter_username'      => 'asdfa',
        ])
            ->assertStatus(302)
            ->assertRedirect('home/something');
    }
}

注意

这里我们捕获了一个不成功的POST请求,包含错误。然后,一个成功的POST请求带有重定向

安装

通过Composer

$ composer require eduardoarandah/autohttptests

如果您使用的是laravel < 5.4,请将其添加到config/app.php中的providers

EduardoArandaH\AutoHttpTests\AutoHttpTestsServiceProvider::class,

用法

php artisan autohttptest:create

它是如何工作的?

当您运行php artisan autohttptest:create yourtest时,它将通过中间件拦截所有请求和响应。

然后分析请求并将其转换为存储在文件storage/autohttptests.txt中的测试。

如果您想了解实时构建该文件的过程,可以使用

tail -f storage/autohttptests.txt

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。