Installation:
composer require --dev bex/behat-test-runner
Add to behat.yml under your test suite:
default:
suites:
default:
contexts:
- Bex\Behat\Context\TestRunnerContext
First Use Case: Create a feature file to test a Behat extension. Example:
Feature: Testing a custom extension
Scenario: Verify custom step execution
Given I have the context:
"""
<?php
use Behat\Behat\Context\Context;
class CustomContext implements Context {
public function __construct() {}
public function customStep() {}
}
"""
And I have the feature:
"""
Feature: Custom step test
Scenario:
Given I execute a custom step
"""
When I run Behat
Then I should not see a failing test
vendor/bex/behat-test-runner/src/Context/TestRunnerContext.php (core logic)tests/ directory (if any).Define Test Contexts:
Use the TestRunnerContext to dynamically generate and test Behat contexts, features, and configurations.
Given I have the context: "<?php ... ?>"
And I have the feature: "Feature: ..."
Simulate Real Scenarios:
Given I have the file "index.html" in document root: "..." # Embed HTML
And I have a web server running on host "localhost" and port "8080"
Extension Testing:
behat.yml snippets:
And I have the behat configuration:
"""
extensions:
MyVendor\MyExtension: ~
"""
Integration with Laravel:
dusk, laravel-behat).CLI Options: Pass PHP CLI options to the nested Behat process:
And I run Behat with options: ["-d", "memory_limit=-1"]
workingDirectory to avoid conflicts with existing files:
contexts:
- Bex\Behat\Context\TestRunnerContext:
workingDirectory: %paths.base%/tmp/test-runner
When I run Behat with verbose output
Temporary Directory Handling:
/tmp/behat-test-runner<...>. If not cleaned up, it may clutter your system.workingDirectory in behat.yml for persistent testing:
workingDirectory: %paths.base%/storage/behat-test-runner
Browser/Server Dependencies:
browserCommand.browserCommand: docker run --rm selenium/standalone-firefox
PHP Version Mismatches:
Output Pollution:
Then I should not see a failing test to filter results.Context Class Loading:
workingDirectory is in your composer.json’s autoload paths or use relative paths.$workingDir = \Bex\Behat\Context\TestRunnerContext::getWorkingDirectory();
When I run Behat with options: ["-vvv"]
TestRunnerContext to override cleanup logic.Custom Steps:
Extend TestRunnerContext to add domain-specific steps. Example:
namespace App\Tests\Behat;
use Bex\Behat\Context\TestRunnerContext;
class CustomTestRunnerContext extends TestRunnerContext {
public function customStep() {
// Add logic here
}
}
Register it in behat.yml:
contexts:
- App\Tests\Behat\CustomTestRunnerContext
Pre/Post-Execution Hooks:
Override beforeRun() or afterRun() in a custom context to modify the nested Behat’s environment.
Configuration Snippets:
Create helper methods to generate reusable behat.yml snippets for your Laravel extensions.
And I have the behat configuration:
"""
extensions:
Behat\MinkExtension:
base_url: http://laravel.test
"""
.env variables to the nested Behat process via CLI options:
And I run Behat with options: ["-d", "APP_ENV=testing"]
behat.yml.How can I help you explore Laravel packages today?