Hyperf 项目如何使用 Pest #6510
Replies: 3 comments 1 reply
-
composer require pestphp/pest --dev --with-all-dependencies 执行这个命令的时候出现包冲突。 You can also try re-running composer require with an explicit version constraint, e.g. "composer require pestphp/pest:*" to figure out if any version is installable, or "composer require pestphp/pest:^2.1" if you know which you need. |
Beta Was this translation helpful? Give feedback.
-
PHP什么版本?hyperf什么版本?phpunit什么版本? |
Beta Was this translation helpful? Give feedback.
-
PHP 8.3.20 (cli), hyperf3.1, phpunit 10.5。 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
什么是 Pest?
Pest 是一个注重简单性的测试框架,经过精心设计,旨在带回 PHP 测试的乐趣。
Pest 和 PHPUnit 是什么关系?
简单理解:套壳。类似 Laravel 与 Symfony 的关系。
集成和开发了一些功能:
Hyperf 项目如何引入 Pest ?
Hyperf 是基于 Swoole/Swow 的协程框架,最大的亮点也是 Coroutine(协程),但是 Swoole 的协程是需要协程环境的(Swow 不需要),也就意味着有些单元测试(TestCase)需要允许在协程环境下,否则会报错。
如:
直接执行,会直接报错。需要套一层协程环境:
这样就不会报错了。但是……每一个都这么“套”一层,挺累的吧。
改造 TestCase
前一步,我们发现每个 Case 都要套一个协程环境,开发体验确实不是特别好,那么我们通过自定义 TestCase 把协程环境自动前置套入到每一个 Case,代码如下:
项目中所有 TestCase 都继承这个类即可。
安装&配置 Pest
前面又是协程环境,又是改造 TestCase,跟 pest 有什么关系?
当然有!除了前面提到的需要支持协程 Case,Pest 还有一个亮点功能:Parallel Testing(并行测试),需要依赖改造过的
TestCase::runTestsInCoroutine
。接下来先安装&配置 pest 吧。
开始 Pest 之旅吧
./vendor/bin/pest --parallel # 制定进程数 ./vendor/bin/pest --parallel --processes=10
Beta Was this translation helpful? Give feedback.
All reactions