The 10 Best Frameworks for Automated PHP Testing

php automated testing

Making of great software includes a well-planned testing strategy to ensure that software is bug free and meets requirements of the end users. There are many different testing categories including unit testing, integration testing, functional testing, system testing, regression testing and more.

Unit testing is generally the first hurdle that any piece of code needs to pass, before it becomes part of a great software. The key purpose of unit testing is to ensure that the smallest testable units of the code are tested standalone, to ensure those are good to use in the overall code base. There are many automated testing tools and frameworks like PHPUnit, Behat andCodeception etc that are used for unit testing as well as functional and acceptance testing of PHP applications. 

Talking a bit more about testing types before we move on to the PHP automated testing tools and frameworks, testing is often categorized as black box testing and white box testing. White box testing includes unit testing, integration testing, regression testing and mutation whereas black box testing includes Acceptance, fuzz, smoke, load testing and stress testing.

On the other side, there are two different approaches (in context of testing) for software development that include Test driven development vs behavior driven development.

Behavior Driven Development

In the behavior driven development approach, user stories take the center stage and the development is done around user stories. User stories are built in a way to be understood by all the stakeholders including business teams, developers and QA team. Behavior driven development approach attempts to close loops the communication and avoid any communication gaps among teams.

Link between Behavior Driven Development and Automated Testing

Automated testing Tools like Behat etc. are designed to pick the user stories written in a defined format and automate the testing to ensure application works the way business understood user stories. The application is automatically tested as it is incrementally built to cover additional user stories.

Test Driven Development

In test driven development, developers write the test scripts, then write the code, test the code, fixes any issues, refactors the code and create necessary comments and documentation. There is a thin line between BDD and TDD with one key difference that user stories in BDD are for all stakeholders while test scripts in TDD are more for developers.

Let us look at various the best PHP tools and frameworks used for automated PHP testing –

PHPUnit

PHPUnit is the top name in the world of PHP Unit testing and stems from the big daddy of unit testing frameworks - xUnit architecture.

PHPUnit is the creation of Sebastian Bergman and focuses completely on developers, it was released it back in 2004 under BSD license. One can get hold of the latest version of PHPUnit from GitHub repository here at – Download PHPUnit.

Getting started with PHPUnit is as easy as it can be, thanks to the great documentation, tons of tutorials and FAQs created by a wonderful PHPUnit developer community. Installation is absolutely easy and can be done using Composer, Phar or Pear.

PHPUnit is best suited for unit testing, in case you are searching something for APIs or functional testing then you might want to look at Codeception, Behat and few other testing tools and frameworks covered later in this article.

Some of the key features of PHPUnit that makes it irresistible include -

  • Mocking
  • Assertions
  • Setup/teardown
  •  Annotations
  • Data providers
  • Code coverage
  • Selenium integration and many more

Check out official website of PHPUnit here at – PHPUnit for Automated PHP Testing

Codeception

Codception focuses on BDD style testing and offers complete set of tools for testing PHP web applications. It can be used for Unit testing, functional testing as well as acceptance testing. Codeception is modular and lets you use different modules as per the need of the project.

Codeception fits perfectly into most of the PHP development workflows since it integrates with popular frameworks like Symfony, Yii, Laraval and Zend etc. Codeception also happens to be one of the most complete tools for testing and comes packed with features like –

  • Domain specific language for creating user centric tests
  •  Browser testing using Chrome, Mozilla, PhantomJS and Selenium WebDriver etc.
  • Simplified REST and SOAP testing - Covers XML and JSON data structures.

Codeception is developed and maintained by Codeception team and is available free to use under open source MIT licenses.

Read more about Codeception here at – Codeception BDD Framework

All set to develop Rest APIs in PHP? The first thing you need to get hold of is a lightweight PHP REST API framework.  A framework in any language is not a... Read More

Behat

Behat is another great framework that focuses on behavior driven development and can be used for auto testing business expectations.  Behat was built back in 2010 by Konstantin kudrvashov using Symfony framework and is available under MIT license, and is free to use.

I love Behat because it works just magical! How? Business users along with development team write user stories using readable language named Gherkin. The user scenarios are saved as .features file which is picked up by the Behat execution engine and the application is auto tested against the user stories. The Behat testing engine ensures that the application meets all the scenarios and user expectations are met.

Behat also happens to be one of the most flexible and extensible tool that can be up scaled by inclusion of custom written extensions. An example extension would be to enhance Behat functionality for APIs testing which is not covered by the core Behat engine.

Check out the official website of Behat here at – Behat Test Automation.

PHPSpec

PHPSpec is also a unit testing framework that works around behavior driven development but it is completely different from how Behat works.  Behat focuses on external behavior of the application while PHPSpec focuses on the internal behavior.

Behat can be categorized as a storyBDD tool and PHPSpec as a specBDD tool. A brilliant customer centric software development can be made an absolute breeze by using specBDD tool like PHPSpec along with a storyBBD tool like Behat.

Read more about PHPSpec here at – PHPSpec Automated PHP Testing

SimpleTest

SimpleTest was created back in 2003 by Marcus Baker for unit testing apps created in PHP programming language. If you are familiar with Junit or PHPUnit then you are at home. All these frameworks support similar structure for unit testing.

Did we talk about regression testing at all? SimpleTest also automates regression testing tasks wherein you can do things to parse HTML pages to test links, buttons, forms submission and validations etc.

SimpleTest also supports frames, proxies, basic authentication and SSL etc.  Though in the recent few years PHPUnit has taken the PHP testing by storm but SimpleTest is still holding the ground and can be grabbed here at GitHub.

Read more about SimpleTest - http://www.simpletest.org/

StoryPlayer

Storyplayer comes packed with many modules to help expedite automated testing of PHP apps and web services. With Storyplayer, there is no domain specific language requirement to write tests, it is all PHP and you can test software written in any of the programming languages.

Storyplayer comes packed with more than 20 ready to use modules that include Amazon AWS, Amazon EC2, Browser, Checkpoint, cURL, HTTP, Host, Graphite and many more.

Check out Storyplayer here at - StoryPlayer

Peridot

Peridot is extremely simple and easy to use tool for automated testing of PHP apps. In BDD world, Peridot focuses on spec based testing. If you are from the JavaScript background and have heard of Jasmine, then you are at home, Peridot does exactly the same in PHP world.

One of the biggest strengths of Peridot is the event driven architecture and availability of many plugins to extend the functionality. You can add test helpers, plugins and customer reporters, databases, WebDrivers, frameworks and anything that you need.

Peridot is lightweight and fast and is a great option for loading and testing big suites. It also offers plugin for concurrency which can be used to run your tests concurrently that can greatly save time when doing slow webDriver or DBTests etc. for example.

Read more about Peridot here at - Peridot

Atoum

Atoum is the creation of Frédéric Hardy and is a modern PHP unit testing framework. Atoum is easy to install, configure and simple to use with Intuitive commands. Autom is complete package in itself and can run on its own without any dependencies on external tools or libraries.

Atoum comes packed with three flavors of execution engines that include 1) inline for sequential test case execution under same process, 2) sequential test case execution with every test case as a new process and 3) concurrent which lets you run all test cases in parallel in separate processes.

Atoum also integrates with many of the third party CI tools including Travis, Jenkins, GitLab CI, Circle CI and PHPCI, on the IDEs side – Atoum integrates seamlessly with top development environments like PHPStorm, Atom, Sublime Text and NetBeans.

Some of the cool extensions that can be used to extend Atoum functionality include bdd-extension to write BDD like test cases, reports-extension, ruler-extension and backfire-extension to use backfire suite for testing in Atoum. Other key features of Autom include Mocks, Virtual File system, reports, auto run modes, loops, php based configuration and many more.

Read more about Autom here at   - http://atoum.org

Mockery

Mockery is more like an extension that can be used with unit testing frameworks like PHPUnit and PHPSpec etc. It is simple mock object framework that can be used in place of something like php-mock-objects library of PHPUnit. What makes mockery more interesting is the readable DSL – Domain specific language.

Read more about Mockery here at - Mockery

Selenium

Selenium brings in specialization in the world of browser testing and is best used for testing web applications. Selenium offers two main tools – Selenium WebDriverr and Selenium IDE.

If you are looking at building regression test to automate browser or distribute scripts across multiple environments, then WebDriver is the component to use. However, if the need is scripts for bug reproduction or automated exploratory testing with scripts then Selenium IDE is the best option.

Selenium testing tools can be used for PHP testing as well as for many other languages that include Ruby, Java and many more. It is available as open source tool and is free to use under Apache 2.0 license. You can install selenium on Linux, OX X as well as on Windows.

Check out more about Selenium here at - Selenium

Another great framework to automate PHP testing is Kahlan, It us a feature rich framework focused on unit and BDD testing. Kahlan eliminates the need for of PECL extensions by letting you stub the code directly, much like how it is done in JavaScript.

Check out Kahlan here at : Kahlan - Automate PHP testing

Conclusion

Any Great software is bug free, competitive and meets end user requirements. Automated testing plays a big role in the whole software development lifecycle and reduces overall effort and time of software build.

Automated testing done with great rigor ensures that the bugs and issues are detected earlier in the development cycle, the coder refactoring is done properly, integration if required goes smoothly, better interfaces and a living documentation of the code that can be referred for anything and everything.

PHP being one of the popular languages has many tools available out there in the wild that automate testing. Tools like Behat, PHPUnit, PHPSpec and Codeception are more or less part of any PHP development workflow.

We hope you enjoyed the article, Do share your views via comments!

About The Author: noeticsunil

Sunil is the founder and contributing editor at noeticforce.com. He writes about anything and everything that makes modern mobile apps, web apps and websites possible. Passionate about coding in any language including Python, Swift, JavaScript, PHP, Java, Android & iOS dev, not excluding CSS/HTML. 

If you like this article, you can connect with noeticforce on Twitter or subscribe to noeticforce feed via RSS.