Home » None » Codeception – BDD-style testing framework for PHP

Codeception – BDD-style testing framework for PHP

Testing is really important. Good test code is needed for ANY application development.
Fortunately, PHP developers have a very good testing framework named PHPUnit.
Codeception is yet another great testing framework for PHP, in *BDD-style*. It provides as many functionality as PHPUnit, then come with BDD-style API which creates a lot of fun to write Test code!

Installation

It’s easy to install Codeception by Composer. For other ways, please follow this documentation. This blog assume you follow installation by Composer.
Note: Codeception 2.0 and higher requires PHP 5.4 installed. Use latest version of Codeception 1.8.x if you run PHP 5.3
After installation, it’s required to run to generate configuration directories and files:

Easy!
Let’s look a bit into just-generated tests/ folder. Besides some configuration files, we can see 3 main directories:
  • functional/
  • acceptance/
  • unit/
Those are where we’ll put test code. Let’s write one.

Test code

First acceptance test
Just execute:

The above command will generate new file named HelloWorldCept.php into /acceptance directory. Let’s write a test code, like writing an essay, to make sure ‘Septech’ is there in frontpage

Wow, ok, failure with red color is expected.
Next, we write quick frontpage, using Silex framework

If you’re not familiar with Silex, take a look at my previous blog on Composer and Silex. The application code of Silex can be found here.
Okay. Now we have application code, let’s run test code again:

Great!

Conclusion

This blog is just a introduction to Codeception. We can do a lot with Codeception, include unit test, functional test, acceptance test, in BDD-style. In that way, we can introduce more stakeholders to (White Box) testing phase, for example Production Owner, QA.., which is so valuable in some context!
Demo code can be found here