Kahlan – Implementing the Goodness of RSpec/Jasmine in PHP Testing

In PHP, there are several testing frameworks you can use to test your applications. Popular ones including PHPUnit, Codeception, Behat and PHPSpec.

PHPUnit helps you to unit test your code, Behat, PHPSpec gives you the ability to do a form of behavioural testing.

Today, I introduce to you Kahlan, A very awesome full-featured Unit and BDD test framework which brings the goodness of RSpec and Jasmine to testing.

I do a lot of Javascripting currently and also worked on Rails app before now. Test tools like Jasmine and Rspec provide me the ability to use the describe-it syntax which makes the testing code very simple and legible to write and understand easily.

Example code


Meanwhile, In PHPUnit,  Similar code would be like this:

Now, I feel this block of code is longer and the results are not so descriptive like that of Jasmine and Rspec when the code is run from the terminal.

Kahlan takes care of all of that. It’s very descriptive. Similar code written with Kahlan:


When we run this spec file using ./bin/kahlan

If successful, results look like this:

If one assertion fails, we will have the result like so:


Awesome!!, See how detailed and descriptive the error message is,…taking into consideration all the description we had written in our test suite.

Note: Kahlan provides monkey patching out of the box also. Is that not awesome? You should definitely try Kahlan out. It’s Great!. :smile:

The source code for this test experiment can be found here. You can go through it, clone it, run composer install and then run the test using ./bin/kahlan --coverage=4

Please if you have any questions or observations, feel free to drop it in the comments section below.



Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.