friends-of-behat/test-context
Composer 安装命令:
composer require friends-of-behat/test-context
包简介
Provides reusable context that helps in testing Behat extensions.
README 文档
README
Provides a reusable Behat context for testing Behat extensions. It lets you run Behat inside Behat - setting up temporary configurations, feature files, and contexts, then asserting on the results.
Installation
composer require friends-of-behat/test-context --dev
Configuration
Register the context in your test suite:
# behat.yml default: suites: default: contexts: - FriendsOfBehat\TestContext\Context\TestContext
// behat.php <?php declare(strict_types=1); use Behat\Config\Config; use Behat\Config\Profile; use Behat\Config\Suite; use FriendsOfBehat\TestContext\Context\TestContext; return (new Config()) ->withProfile( (new Profile('default')) ->withSuite( (new Suite('default')) ->withContexts(TestContext::class) ) );
How It Works
Each scenario gets an isolated temporary directory that is created before and cleaned up after every scenario. When you use the setup steps, files are written into this directory.
When I run Behat executes a real Behat process in that directory, and the assertion steps check its exit code and output.
This means scenarios are fully independent — file changes from one scenario never leak into another.
Usage Examples
Testing a simple extension
Feature: My extension In order to ensure my extension works As a Behat extension developer I want to run Behat with my extension loaded Scenario: Extension registers successfully Given a Behat configuration containing: """ default: extensions: My\Extension: ~ """ And a feature file containing: """ Feature: Smoke test Scenario: It works Then it passes """ And a context file "features/bootstrap/FeatureContext.php" containing: """ <?php use Behat\Behat\Context\Context; use Behat\Step\Then; class FeatureContext implements Context { #[Then('it passes')] public function itPasses() {} } """ When I run Behat Then it should pass
Quick smoke tests with shorthand steps
Feature: Quick checks Scenario: Passing scenario Given a feature file with passing scenario When I run Behat Then it should pass with: """ 1 scenario (1 passed) 1 step (1 passed) """ Scenario: Failing scenario Given a feature file with failing scenario When I run Behat Then it should fail with: """ 1 scenario (1 failed) 1 step (1 failed) """
Asserting on error output
Scenario: Unknown extension causes a configuration error Given a Behat configuration containing: """ default: extensions: Unknown\Extension: ~ """ When I run Behat Then it should fail with "ExtensionInitializationException"
Available Steps
Setup
| Step | Description |
|---|---|
Given a Behat configuration containing: |
Creates a temporary behat.yml (or behat.php for Behat 4) |
Given a feature file "path" containing: |
Creates a feature file at the given path |
Given a feature file containing: |
Creates a feature file with an auto-generated path |
Given a context file "path" containing: |
Creates a PHP context file at the given path |
Given a file "path" containing: |
Creates any file at the given path |
Given a feature file with passing scenario |
Generates a ready-made passing scenario with context |
Given a feature file with failing scenario |
Generates a ready-made failing scenario with context |
Given a feature file with scenario with missing step |
Generates a scenario with an undefined step |
Given a feature file with scenario with pending step |
Generates a scenario with a pending step |
Execution
| Step | Description |
|---|---|
When I run Behat |
Runs Behat in the temporary working directory |
Assertions
| Step | Description |
|---|---|
Then it should pass |
Asserts Behat exited with code 0 |
Then it should pass with "text" |
Asserts Behat passed and output contains text |
Then it should fail |
Asserts Behat exited with a non-zero code |
Then it should fail with "text" |
Asserts Behat failed and output contains text |
Then it should end with "text" |
Asserts output contains text (regardless of exit code) |
All with variants also accept multiline pystrings.
friends-of-behat/test-context 适用场景与选型建议
friends-of-behat/test-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.36k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2016 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 friends-of-behat/test-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 friends-of-behat/test-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 23.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 15
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-28