- Can Behat replace PHPUnit for all Laravel tests, or is it just for acceptance criteria?
- Behat is best suited for acceptance criteria and end-to-end workflows, not unit testing. Use PHPUnit for unit/integration tests and Behat for Gherkin-based scenarios. Mixing them avoids maintenance debt from overusing .feature files.
- How do I install Behat in a Laravel project via Composer?
- Run `composer require --dev behat/behat` to install. For Laravel-specific helpers, add `behat/laravel-extension` via `composer require --dev behat/laravel-extension`. Execute tests with `vendor/bin/behat` from your project root.
- Which Laravel versions does Behat 3.x officially support?
- Behat 3.x works with Laravel 8+ (PHP 8.1+) and Laravel 9/10 (PHP 8.2+). Check your Laravel version’s PHP requirements—Behat drops PHP 8.1 support in 4.0. Use `composer require behat/behat:^3.15` for stability.
- How can I test Laravel API endpoints with Behat?
- Use the `guzzlehttp/guzzle` extension or Behat’s HTTP context with `behat/mink-extension`. For Laravel, inject the app’s HTTP client in step definitions or use `behat/laravel-extension` for auth helpers like Sanctum tokens.
- Are there performance concerns running Behat in CI for Laravel?
- Yes—Behat is slower than PHPUnit. Mitigate this by caching `vendor/` in CI, parallelizing scenarios with `--tags`, and avoiding overly granular steps. Use `DatabaseTransactions` to speed up database-heavy tests.
- How do I handle Laravel’s database migrations in Behat tests?
- Use Laravel’s `RefreshDatabase` or `DatabaseTransactions` traits in your context classes. For isolated tests, wrap scenarios in `BeforeScenario` hooks to refresh the database or roll back migrations.
- What’s the best way to test Laravel Livewire components with Behat?
- Combine `behat/mink-extension` with `facebook/webdriver` for browser automation. Use Mink’s JavaScript execution to trigger Livewire events, then assert UI changes. Avoid direct DOM manipulation—test workflows instead.
- How do I mock Laravel events (e.g., Horizon queues) in Behat scenarios?
- Use Laravel’s `Event::assertDispatched()` in step definitions or mock the event dispatcher in contexts. For queues, assert job dispatching via `Queue::fake()` or mock Horizon workers in `BeforeScenario` hooks.
- What alternatives to Behat exist for Laravel BDD testing?
- Consider Pest’s BDD syntax (for simpler workflows) or Laravel’s built-in `Http::fake()` for API tests. For GUI testing, Cypress or Playwright may be better than Mink. Behat’s strength is Gherkin collaboration, though.
- How do I prepare for Behat 4.0’s breaking changes in a Laravel project?
- Monitor the [Behat 4.0 migration guide](https://docs.behat.org) for Symfony 8+ dependencies. Update `behat/laravel-extension` early, test with PHP 8.2+, and refactor step definitions to avoid Gherkin parser changes. Plan a CI test phase before upgrading.