Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webkenny's avatar

Behat vs. Testing with PHPUnit in Laravel

I'm a little new to Laravel. Only a few months. Not as new to PHP frameworks and our friend, Behat. I'm having a bit of a conceptual tangle I am hoping to get some feedback on.

Reading the documentation on application testing it looks as though Laravel ships with a pretty fluent library for doing what I would do in other frameworks with Behat and Mink. (i.e. the ->see("something"); method looks awfully similar to the MinkContext for testing text on a page.

I have always known PHPUnit as just that. Unit testing. Behat as your typical BDD framework. So my question is this: When to use which? Is there some compelling reason to use Behat over Laravel's built-in testing?

I appreciate any conceptual help on this. I'm fine with either. I'm just trying to get by my purist notions of what PHPUnit has always been seen as to me.

(Side note: I'm building an API if that helps inform the answers)

0 likes
3 replies
davorminchorov's avatar

PHPUnit is usually used for Unit and Integration tests and Behat is usually used for Acceptance tests.

Corez64's avatar
Corez64
Best Answer
Level 37

The big advantage with Bahat is that it uses Gherkin to write the tests well you can share them with your client and they should be able to understand your tests.

This can be useful for 2 reasons I can think of right now:

  1. You can write your spec in the form of Bahat tests and you can literally test your code against tests that your client has agreed upon.
  2. if it turns out you didn't understand some complicated maths that is part of your client app you can talk to the client, show them your tests and they can help you see where the problem is as they can understand your tests well enough to help.

Jeff made a video called The Needs of the Business which covers this.

However as you pointed out you are writing an API the value of this is somewhat reduced because the API is fairly technical anyway and is less well suited for development in this way. Unless you, as a developer, are more comfortable using Behat I would stick with PHPUnit.

webkenny's avatar

Thanks to both of you for great guidance. So it seems the real reason to use Behat, as I have, is when dealing in client tests that humans may need to read (or QA may need to test). But where I'm primarily working on an API to be used by devs, it seems PHPUnit would suit me best. I'll watch that Needs of the Business as well. Thanks again!

Please or to participate in this conversation.