mgjgid / laravel-http-tests-generator
自动生成 Laravel 的 HTTP 测试
dev-master
2024-03-05 09:21 UTC
Requires
- php: ^8.0
- illuminate/support: ^10.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-05 10:21:25 UTC
README
通过点击 Laravel 应用生成 HTTP 测试
只需执行命令,即可将您的操作记录为 HTTP 测试
php artisan autohttptest:create
该命令将 拦截 您的请求,并将响应转换为测试。
完成后,您的测试将保存在 tests/Feature/ 目录中
视频演示
测试了什么?
- 作为同一用户执行的请求
- 使用相同的动词(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)。请参阅 许可证文件 了解更多信息。