friends-of-behat/test-context
Reusable Behat context for testing Behat extensions by running “Behat inside Behat”. Creates isolated temp projects per scenario, writes configs/features/contexts, runs a real Behat process, and lets you assert on exit codes and output.
Installation:
composer require friends-of-behat/test-context --dev
Add to your behat.yml or behat.php:
# behat.yml
default:
contexts:
- FriendsOfBehat\TestContext\Context\TestContext
Or programmatically:
// behat.php
return (new Config())->withProfile(
(new Profile('default'))
->withSuite(
(new Suite('default'))
->withContexts(TestContext::class)
)
);
First Use Case: Test a Behat extension by creating a feature file and context in a scenario:
Feature: Extension Test
Scenario: Basic extension test
Given a Behat configuration containing:
"""
default:
extensions:
My\Extension: ~
"""
And a feature file containing:
"""
Feature: Test
Scenario: Test
Then it passes
"""
And a context file "features/bootstrap/MyContext.php" containing:
"""
<?php
use Behat\Behat\Context\Context;
use Behat\Step\Then;
class MyContext implements Context {
#[Then('it passes')] public function itPasses() {}
}
"""
When I run Behat
Then it should pass
Extension Testing:
Given a Behat configuration containing to define extension dependencies.Given steps.When I run Behat + Then assertions.Isolated Scenarios:
Scenario: Invalid extension config
Given a Behat configuration containing:
"""
default:
extensions:
Invalid\Extension: ~
"""
When I run Behat
Then it should fail with "ExtensionInitializationException"
Smoke Testing:
Scenario: Smoke test
Given a feature file with passing scenario
When I run Behat
Then it should pass
Artisan::call()) to mock dependencies in contexts.TestContext to add domain-specific setup steps (e.g., Laravel-specific configurations).File Paths:
"features/bootstrap/MyContext.php") for portability.Behat Version Mismatch:
behat.php for Behat 4).behat.yml for Behat 3 or earlier.Output Assertions:
Then it should pass with "text" is strict—use Then it should end with "text" for partial matches.Symfony Autowiring:
--verbose to Behat commands in your test scenario to inspect generated files.$this->getWorkingDirectory() in custom contexts to debug file paths.Reusable Snippets:
Background:
Given a Behat configuration containing:
"""
default:
extensions:
My\Extension: ~
"""
Reduce boilerplate for extension tests.
Multiline Strings:
Use """ for YAML/feature file content to avoid escaping issues.
Extension Points:
Override TestContext to add Laravel-specific steps:
class LaravelTestContext extends TestContext {
public function __construct() {
parent::__construct();
$this->registerStep('Given a Laravel service provider', function() {
// Custom logic
});
}
}
Performance:
FileCache).Given steps—keep them lightweight.Edge Cases:
Scenario: Empty config
Given a Behat configuration containing: ""
When I run Behat
Then it should pass
How can I help you explore Laravel packages today?