It’s a brand new week and we know your tech brains are a-buzzing! This week we have a special guest blog from UKFast techie George who explains Codeception, what it means for PHP developers across the world and tips to make your coding life easier!
Codeception is a PHP testing framework used by developers to check the sites they work on. It splits tests up into individual suites: unit tests, functional tests and acceptance tests. It has been around for a while now (with the first commit appearing around seven years ago) and has a well-established and active community.
Laravel also has a great set of tools for testing apps – PHPUnit with Dusk pretty much gives you everything you’d need to write a full range of tests. However, Codeception working alongside Laravel provides some really handy extras that make writing tests even simpler.
Codeception provides out-of-the-box solutions for three suites, each with different purposes and designed to be run at different intervals.
The Codeception team recommend using WebDriver with ChromeDriver or GeckoDriver as, although it’s the heaviest and hardest to install, it’s the most stable and feature complete so your tests have the most realistic and reliable output.
A broad comparison of the three is:
|Scope||Classes/methods||App||End to End|
Codeception provides great support for working with CI services. Firstly, it prints test results to stdout which makes it immediately compatible with pretty much all CI services. Beyond this, it outputs all debug data to an output folder. This is really handy for CI as you can zip up this folder and save it as an artefact, so if your test fails, you can analyse the output and see what went wrong. In the case of acceptance tests, you can even save a screenshot of what happened when the tests failed. It also supports running acceptance tests on headless servers, using Selenium and ChromeDriver so you don’t need a special VM to run acceptance tests.
There is also good support for Docker, there is already a Codeception Docker image on Docker Hub, which provides basic functionality out of the box. Find it here.
You can also instantiate a standalone selenium server using Docker, here.
Codeception provides a variety of modules straight out of the box. They roughly fall into one of three categories: framework drivers, external services and generic helpers.
The framework drivers allow Codeception to run functional test across different frameworks, not just Laravel. Currently supported frameworks are: Laravel 5, Lumen, ZF1, ZF2, Yii1, Yii2, Symfony, Silex and Zend Expressive. This is useful, as you can migrate tests between frameworks, and you can use the same tools across different frameworks and environments.
External service modules provide helpers and reduce boilerplate code when making assertions on other services. Examples of these drivers include: Facebook, Redis, Memcached, MongoDb, Apc, AMQP. The main purpose of these drivers is to reduce the amount of boilerplate you have to write when writing tests that interact with these services.
Generic modules provide modules that are broader in purpose. For example: Filesystem, FTP, Cli, Mock, and DataFactory. These all do very different things, but each provides access to test methods that most suites are likely to require at some point.
The Codeception module system is one of the main benefits of using Codeception over just PHPUnit and Dusk. It hugely reduces the amount of code you have to write as well as the amount of time you spend writing tests.
Although Laravel already has some excellent testing tools, Codeception is better established and provides a much more feature-complete testing experience. The documentation is very well-written and goes into lots of detail, there are loads of community supported drivers and modules, and the framework itself promotes simple, reusable tests. When writing tests for your next Laravel project, I would thoroughly recommend checking out Codeception!
Find out more about UKFast and how we’re always
upskilling our team.