danon910 / blitzy
Laravel 测试生成器包
1.0.0
2024-06-13 13:21 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.0
This package is auto-updated.
Last update: 2024-09-27 11:31:43 UTC
README
Blitzy 是一个轻量级的 Laravel 包,用于自动化测试生成。它旨在加速测试过程并使开发者生活更加轻松。
安装
1. 使用 composer 安装包
composer require danon910/blitzy --dev
2. 在 config/app.php
中添加 ServiceProvider
\Danon910\blitzy\Providers\BlitzyProvider::class,
3. 发布配置
php artisan vendor:publish --provider="Danon910\blitzy\Providers\BlitzyProvider" --tag=config
配置
在使用 Blitzy 之前,您应该根据需求自定义其行为,您可以编辑位于 config/blitzy.php
的配置文件。
该包做什么?
该包在 tests
文件夹中生成具有正确命名空间和简单结构的 Test
和 Trait
文件,以便编写真正的测试。
命令
生成简单测试
php artisan blitzy:generate "{path}" --type=smoke --force
生成更精确的测试
php artisan blitzy:generate "{path}" --type=smoke --feature=Post --methods=index,show --force
必选参数
{path}
被测试类的路径
--type
烟雾测试 / 集成测试 / 单元测试
可选参数
--feature
将保存在文档块中的功能名称
--methods
提供要解析的方法(例如 index,show)
--force
如果您想要覆盖已生成的现有测试文件
用法
生成烟雾测试
php artisan blitzy:generate "App\Http\Controllers\PostController" --type=smoke --force
生成集成测试
php artisan blitzy:generate "App\Services\PostService" --type=integration --force
生成单元测试
php artisan blitzy:generate "App\Services\PostService" --type=unit --force
示例
烟雾测试
<?php /** This test file was automatically generated by Blitzy. */ declare(strict_types=1); namespace Tests\Smoke\App\Http\Controllers\PostController\Index; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; class PostControllerTest extends TestCase { use PostControllerTrait; use DatabaseTransactions; /** * @feature Post * @scenario Index * @case Happy path * * @expectation Return valid json structure * * @test */ public function index_happyPath_returnValidJsonStructure(): void { $this->markTestSkipped("Test generated automatically!"); // TODO: Check this test! // GIVEN // WHEN $response = $this->getJson(route("posts.index")); // THEN $response->assertOk(); $response->assertJsonStructure($this->getExpectedJsonStructure()); } }
集成测试
<?php /** This test file was automatically generated by Blitzy. */ declare(strict_types=1); namespace Tests\Integration\App\Services\PostService\FindById; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; class PostServiceTest extends TestCase { use PostServiceTrait; use DatabaseTransactions; /** * @feature Post * @scenario Find by id * @case Happy path * * @expectation TODO * * @test */ public function findById_happyPath_todo(): void { // GIVEN $properties = []; $id = 1; // WHEN $result = $this->getTestedClass($properties)->findById($id); // THEN // TODO } }
单元测试
<?php /** This test file was automatically generated by Blitzy. */ declare(strict_types=1); namespace Tests\Unit\App\Services\PostService\FindById; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; class PostServiceTest extends TestCase { use PostServiceTrait; use DatabaseTransactions; /** * @feature Post * @scenario Find by id * @case Happy path * * @expectation TODO * * @test */ public function findById_happyPath_todo(): void { // GIVEN $properties = []; $id = 1; // WHEN $result = $this->getTestedClass($properties)->findById($id); // THEN // TODO } }
更新日志
有关最近更改的更多信息,请参阅 更新日志。