linio / behat-web-api-extension
behat/web-api-extension的维护分支,增加了扩展和支持。
3.1.0
2023-09-06 16:37 UTC
Requires
- php: >=7.3 || ^8.0
- behat/behat: ^3.10
- guzzlehttp/guzzle: ~6 || ~7
- phpunit/phpunit: 6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- slim/psr7: ^1.5
- slim/slim: ^4.0
- symfony/process: ^4,2 || ^5.0 || ^6.0
README
为Behat 3提供REST API的测试。这是behat/web-api-extension的维护分支,增加了额外功能和长期支持。
使用方法
只需将其添加到您的composer开发依赖中
$ composer require --dev linio/behat-web-api-extension
并激活您的扩展
# behat.yml
default:
# ...
extensions:
Behat\WebApiExtension: ~
私有到保护
原始behat/web-api-extension
库中一个棘手的问题是对私有属性和方法的大量使用,这阻止了您轻松地扩展它。这个分支通过将所有内容移动到protected
来修复了这个问题。
占位符支持
这个分支的新特性之一是能够使用正则表达式中的占位符来帮助您测试变化多端的输入或输出。例如
Scenario: Sending values with placeholders
Given a file named "features/send_values.feature" with:
"""
Feature: Exercise WebApiContext data sending
In order to validate the send request step
As a context developer
I need to be able to send a request with values in a scenario
Scenario:
When I send a POST request to "echo" with values:
| name | name |
| pass | pass |
Then the response should contain "POST"
And the response should contain json:
'''
{
"name" : "name",
"pass": "%[a-z]+%"
}
'''
"""
When I run "behat features/send_values.feature"
Then it should pass with:
"""
...
1 scenario (1 passed)
"""
API返回动态内容的响应是很常见的。例如UUIDs、时间戳、生成的密码等。不幸的是,这些都使得编写场景变得有点挑战。使用占位符,您可以通过正则表达式轻松测试它们是否以有效的格式返回,但仍可以保持变量。
其他占位符示例
{
"timestamp": "%^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2,}[\\-\\+][0-9]{2}:[0-9]{2}$%",
"uuid": "%^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$%"
}
测试
$ composer install
$ php -S 0.0.0.0:8080 -t testapp &
$ vendor/bin/behat -f progress
版权
版权 (c) 2014 Konstantin Kudryashov (ever.zet)。有关详细信息,请参阅LICENSE。