pest
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePest
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 and
assertForbiddeninstead of usingassertNotFoundor 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>assertStatus(403)
- 在对响应断言状态码时,应使用特定方法如 和
assertForbidden,而非assertNotFound这类写法,例如: <code-snippet name="Pest Example Asserting postJson Response" lang="php"> it('returns all', function () { $response = $this->postJson('/api/docs', []); $response->assertSuccessful(); }); </code-snippet>assertStatus(403)
Mocking
模拟(Mocking)
- Mocking can be very helpful when appropriate.
- When mocking, you can use the Pest function, but always import it via
Pest\Laravel\mockbefore using it. Alternatively, you can useuse function Pest\Laravel\mock;if existing tests do.$this->mock() - You can also create partial mocks using the same import or self method.
- 在合适的场景下,模拟(Mocking)会非常有用。
- 进行模拟时,可以使用 这个Pest函数,但在使用前必须通过
Pest\Laravel\mock导入。或者,如果已有测试使用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.
- 在Pest中使用数据集来简化存在大量重复数据的测试。这种情况在测试验证规则时很常见,因此在编写验证规则的测试时可以考虑采用这种方案。
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/