Behat is a popular behaviour-driven PHP testing framework. The tests we can write with Behat look rather like stories than code. Behat uses the StoryBDD subtype of behaviour-driven development (the other subtype is SpecBDD).
Kahlan is a full-featured BDD testing framework that makes it possible to write Unit Tests using the describe-it syntax. It embraces the KISS (Keep It Simple, Stupid) design principle. Kahlan requires at least PHP 5.5.
Why Testing Is Important
It has a small code base, it’s said to be about 10 times smaller than PHPUnit, and it has loads of features that provide us with an extensible and customizable testing workflow.
Atoum is an intuitive and modern PHP testing framework that allows us to run unit tests. It simplifies test development, and as it’s a young framework it makes use of some newer capabilities that were introduced in PHP 5.3 (it can‘t be used with older PHP versions) to provide us with a quick and easy-to-understand testing process.
Have you ever spent long hours debugging your PHP code? Well, for most programmers it’s probably not the most fascinating challenge, but there’s a solution that can help us shorten this tedious task. Automated testing can significantly improve the workflow of PHP development by allowing pre-written tests to drive the development process.
PHPUnit is the best-known testing framework for writing Unit Tests for PHP apps. Unit tests take small portions of code called units and test them one by one. With the help of PHPUnit we can conduct test-driven development.
The main difference between test-driven and behaviour-driven testing is in the syntax of the testing code: TDD uses proper PHP code in the testing files, while BDD uses human-readable sentences that describe the behaviour of each feature and can be understood by non-technical stakeholders too.
- more maintainable code
- facilitated refactoring
- less security issues
- less bloated code base (adapting the application code to the pre-written test code will make us omit superfluous parts)
- better performance
What Is Automated Testing?
Peridot uses the describe-it syntax to establish a clear and readable testing language that clearly describes how our application code should behave.
Writing tests for our code can spare precious debugging time and hours of headache. When untested code goes into production, the reviews we’ll get probably won’t be so nice. So what can we do?
Test-Driven Development (TDD) vs. Behaviour-Driven Development (BDD)
There’s an important thing though that we always need to keep in mind. Automated testing may be powerful, but it can never substitute beta testing – tests done by real humans who will be the future users of the application.
SimpleTest is an easy-to-use PHP unit testing framework in Test-Driven Development style, it can be seen as an alternative for PHPUnit. SimpleTest supports SSL, forms, proxies, frames and basic authentication, and it allows us to test common PHP tasks quickly. For sample test cases check out the tutorials of the developer team.
With that in mind, let’s take a look at 10 powerful automated testing frameworks For PHP you can use.
In this post we will try to understand why automated testing is so cool, how it works, and what are the best testing frameworks you can start out with.
10 PHP Automated Testing Frameworks
Novice PHP developers tend not to write tests for their code. Most of us began our careers by testing new features and functions we had just written in the browser window one by one, and when something went wrong we were clueless about what to do.
Other than saving time and properly preparing our app for production, testing has many other adventages too, such as:
PHPSpec also follows the behaviour-driven testing approach, but its other subtype called SpecBDD. With PHPSpec we need to write the specifications first that describe how the application code will behave. It was also inspired by a Ruby testing framework called RSpec.
The two main approaches that automated testing frameworks use are Test-Driven Development (TDD) and Behaviour-Driven Development (BDD). In both, the development process is driven by testing, that means that tests are written before the application code.
Peridot is a lightweight, extensible testing framework for PHP. It features an event-driven architecture that allows testers to easily customize the framework via plugins and reporters.
Atoum ensures a high level of security during test execution, as it isolates each test method in its own PHP process.
Selenium is a robust tool that has its own WebDriver API that can drive a browser natively as though a real user would use it either locally or on a remote machine. Selenium is an excellent tool for testing more mature web applications.
A TDD Test looks like this in the code editor, it’s just regular object-oriented PHP code:
The trick is that the testing code needs to be written before the application code. This way we can ensure from the very beginning that the code we write works properly. The testing code will go into separate files. If we later bump into any errors in our PHP app, we don’t need to do anything else, just run our tests that hopefully will give us proper hints about the problems.
The framework was inspired by the Cucumber project that’s a testing framework for the Ruby programming language.
A BDD Test is similar to this, non-programmers can also make sense of it:
Of course, we can write our tests manually, but after a while it can be tedious and time-consuming with many repetitive tasks. Manual testing can be replaced by automated testing in which we use specific softwares that will do the tiresome work for us, and we can spend more time with creating the logic of the testing code.
Codeception doesn’t only enable us to write Unit Tests, but also Functional and Acceptance Tests. These two latter tests the PHP app as a whole with all features tied together, not as units. Codeception allows us to enable and configure different modules according to our development needs.
It can be used via the command line, and it provides us with a handy TestCase class that we can extend according to our needs. PHPUnit also allows developers to use pre-written assertion methods to assert that the app behaves a certain way.
PHP is luckily a quite popular programming language, so there are many automated testing frameworks we can choose from.
It’s integrated with many PHP development frameworks such as Symfony2, Laravel4, Yii, Phalcon, and the Zend Framework. This means that we can use a PHP automation testing framework and a PHP development framework together to establish a superefficient development workflow.
Storyplayer is a full-stack testing framework that makes it possible to write end-to-end tests for an entire platform. Storyplayer has support for creating and destroying test environments on demand. It follows the TDD testing approach, and allows us to write functional tests that can check an application as a whole.
Selenium is a sophisticated testing framework that automates browsers. This means it’s possible to write User Acceptance Tests that examine the entire app as a whole.
Automated testing frameworks empower us to write higher quality PHP code, and due to the popularity of the language we have many choices, so we can opt for the one that meets our development needs the best.
Probably the best idea is to establish a productive testing workflow in which we need to write our own testing code that checks if our application code works properly.