借助laravel Pest对待写的测试进行头脑风暴

156 阅读1分钟

Pest给了我们进行优雅测试的可能,它的test文件就像是一个涂鸦板,可以让我们在有想法的时候随写随画、即兴表达,没有一大堆的程式化羁绊。

举个栗子,在laravel的默认ExampleTest.php文件里,如果用Pest写:

it('returns a successful response', function () {
    $response = $this->get('/');
 
    $response->assertStatus(200);
});
 
it('requires a valid email')->todo();
it('detects Gmail addresses with a "+" as a non-unique email.')->todo();
it('requires a strong password')->todo();

运行一下pest的命令行,结果如下: image.png

也可以加上--dirty参数,来只是跑改动过的test: image.png

但是,如果我们用传统的phpunit类,就得下面这么一大堆了:

namespace Tests\Feature;
 
use Tests\TestCase;
 
class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     */
    public function test_the_application_returns_a_successful_response(): void
    {
        $response = $this->get('/');
 
        $response->assertStatus(200);
    }
 
    /** @test */
    public function it_requires_a_valid_email()
    {
        $this->markTestIncomplete('requires a valid email');
    }
 
    /** @test */
    public function it_detects_gmail_address_with_a_plus_as_non_unique()
    {
        $this->markTestIncomplete('detects Gmail addresses with a "+" as a non-unique email.');
    }
 
    /** @test */
    public function it_requires_a_strong_password()
    {
        $this->markTestIncomplete('requires a strong password');
    }
}

而且它的返回也特别没营养:

$ phpunit
 
..III                                                               5 / 5 (100%)
 
OK, but there are issues!
Tests: 5, Assertions: 2, Incomplete: 3.

当然了,我们也可以使用artisan test来跑pest: image.png

好了,下次当你想写一个什么测试,但是临时又没时间具体写,或者你们团队需要头脑风暴、集中畅想可以有哪些测试的时候,记得用上这个pest的todo方法哦。

该篇是pilishen老师即将推出的《Laravel Pest TDD优雅测试开发》课程的扩展文章,敬请期待~欢迎来公开QQ群做客:109256050(公开课@pilishen.com)