pest

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Pest

Pest

Instructions

说明

  • Use
    php artisan make:test --pest {name}
    .
  • Pest tests look and behave like this: <code-snippet name="Basic Pest Test Example" lang="php"> it('is true', function () { expect(true)->toBeTrue(); }); </code-snippet>
  • 使用
    php artisan make:test --pest {name}
    命令。
  • Pest测试的外观和行为如下所示: <code-snippet name="Basic Pest Test Example" lang="php"> it('is true', function () { expect(true)->toBeTrue(); }); </code-snippet>

Examples

示例

Pest Assertions

Pest 断言

  • When asserting status codes on a response, use the specific method like
    assertForbidden
    and
    assertNotFound
    instead of using
    assertStatus(403)
    or similar, e.g.: <code-snippet name="Pest Example Asserting postJson Response" lang="php"> it('returns all', function () { $response = $this->postJson('/api/docs', []); $response->assertSuccessful(); }); </code-snippet>
  • 在对响应断言状态码时,应使用特定方法如
    assertForbidden
    assertNotFound
    ,而非
    assertStatus(403)
    这类写法,例如: <code-snippet name="Pest Example Asserting postJson Response" lang="php"> it('returns all', function () { $response = $this->postJson('/api/docs', []); $response->assertSuccessful(); }); </code-snippet>

Mocking

模拟(Mocking)

  • Mocking can be very helpful when appropriate.
  • When mocking, you can use the
    Pest\Laravel\mock
    Pest function, but always import it via
    use function Pest\Laravel\mock;
    before using it. Alternatively, you can use
    $this->mock()
    if existing tests do.
  • You can also create partial mocks using the same import or self method.
  • 在合适的场景下,模拟(Mocking)会非常有用。
  • 进行模拟时,可以使用
    Pest\Laravel\mock
    这个Pest函数,但在使用前必须通过
    use function Pest\Laravel\mock;
    导入。或者,如果已有测试使用
    $this->mock()
    ,也可以沿用该方式。
  • 你也可以使用相同的导入方式或自身方法创建部分模拟(partial mocks)。

Datasets

数据集(Datasets)

  • Use datasets in Pest to simplify tests which have a lot of duplicated data. This is often the case when testing validation rules, so consider going with this solution when writing tests for validation rules.
<code-snippet name="Pest Dataset Example" lang="php"> it('has emails', function (string $email) { expect($email)->not->toBeEmpty(); })->with([ 'james' => 'james@laravel.com', 'taylor' => 'taylor@laravel.com', ]); </code-snippet>
  • 在Pest中使用数据集来简化存在大量重复数据的测试。这种情况在测试验证规则时很常见,因此在编写验证规则的测试时可以考虑采用这种方案。
<code-snippet name="Pest Dataset Example" lang="php"> it('has emails', function (string $email) { expect($email)->not->toBeEmpty(); })->with([ 'james' => 'james@laravel.com', 'taylor' => 'taylor@laravel.com', ]); </code-snippet>

Pest 4

Pest 4

  • Pest v4 is a huge upgrade to Pest and offers: browser testing, smoke testing, visual regression testing, test sharding, and faster type coverage.
  • Browser testing is incredibly powerful and useful for this project.
  • Browser tests should live in
    tests/Browser/
    .
  • Pest v4是Pest的一次重大升级,新增了:浏览器测试、冒烟测试、视觉回归测试、测试分片以及更快的类型覆盖率检测功能。
  • 浏览器测试功能极为强大,对本项目非常有用。
  • 浏览器测试应存放在
    tests/Browser/
    目录下。