Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit & Behat; complementing or alternatives?

I have been looking around SO and Google, but I couldn't really get a definitive answer.

PHPUnit is a framework for unittesting, like JUnit. I use it, also in combination with the Selenium-extension for functional testing. When browsing around I see Behat/Mink keeps on getting mentioned. But I do not completely understand how Behat fits in here.

With Behat you write scenarios in human-readable format. Behat can then translate that into skeleton classes for a new project? But does it also provide skeleton classes for unittesting? Do you write unittests using Behat, or you would use PHPUnit / SimpleTest for those?

But then Behat/Mink does replace PHPUnit_Selenium-extension for functional testing?

Do you use Behat only for new projects, or can it also be adapted to existing projects?

like image 620
qrazi Avatar asked Feb 12 '13 08:02

qrazi


People also ask

Why do we use PHPUnit?

PHPUnit is used for unit testing your PHP projects. The word Unit refers to a block of code, method or an individual or independent class. Unit testing is a software testing process in which code blocks are checked to see whether the produced result matches the expectations.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.

How does PHPUnit work?

PHPUnit is a framework independent library for unit testing PHP. Unit testing is a method by which small units of code are tested against expected results. Traditional testing tests an app as a whole meaning that individual components rarely get tested alone.


1 Answers

I would consider Behat to be complementary to PHPunit. I mean complementary by using Behat for testing behavior, PHPUnit for testing code. Using BDD/TDD, one would first write a test based on a user-story. To make the test pass, you would typically write several PHPUnit tests using TDD to create the application which will be able to pass the BDD test.

BDD should test behavior and be unaware of the code of the application, like entities/controllers/interfaces. You should be able to swap out the complete application with another one and still pass all tests when the behavior of the application is the same.

TDD, besides other things like your mind, should guide your structure of classes and interaction between them. Besides that, they prove your methods do what they should do, makes the intent clear and makes it possible to refactor without fear.

like image 51
Jop van Raaij Avatar answered Oct 13 '22 03:10

Jop van Raaij