PHPUnit tests please
October 5, 2015
Making Codeception and PHPUnit transition easier with WP Browser.
WordPress automated testing
The team in charge of WordPress development (thank you all by the way) uses an automated testing process to test the core code base.
The same test potentialities are available to any one using WP-CLI to scaffold plugins code using the wp scaffold plugin
and wp scaffold plugin-tests
command.
WP-CLI will take care of setting up the files PHPUnit needs to know where and how to run the tests.
Codeception and WP Browser
I've put together wp-browser to ease my Codeception testing experience while working with WordPress and I end up using the tool every day.
In stricter testing terms WordPress "unit" tests are functional tests; PHPUnit will run unit and functional tests without a glitch but that's a distinction it is important to know.
WP Browser packs the WP Loader module to be able to wrap WordPress "unit" tests and run them in a "Codeception way".
I will use Codeception for anything I want to TDD and when asked to port tests that I usually run in Codeception over to PHPUnit I would go over and create the needed phpunit.xml
and bootstrap.php
files.
Since I know how to do it by hand I've implemented the function as part of the wp-browser library.
wp-browser 1.7.0
The feature is important enough to be worth a version bump.
The newly added wpcept generate:phpunitBootstrap
command will read the codeception.yml
file for the tests and create the files needed to run the phpunit
command from the plugin or theme root folder.
As an example here is a generated phpunit.xml
file
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="tests/phpunit-bootstrap.php"
cacheTokens="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
verbose="false">
<testsuites>
<testsuite name="Functional">
<directory suffix="Test.php" phpVersion="5.4.0" phpVersionOperator=">=">tests/functional</directory>
</testsuite>
</testsuites>
</phpunit>
and the corresponding tests/bootstrap.php
file
<?php
// include Composer autoload files
require_once dirname(__FILE__) . '/../vendor/autoload.php';
require_once dirname(__FILE__) . '/../vendor/lucatume/wp-browser/autoload.php';
// load WPLoader and let it do the heavy lifting of starting WordPress and requiring the plugins
$configFile = dirname(__FILE__) . '/../codeception.yml';
$config = Symfony\Component\Yaml\Yaml::parse($configFile);
$moduleContainer = new Codeception\Lib\ModuleContainer(new Codeception\Lib\Di(), $config);
$loader = new Codeception\Module\WPLoader($moduleContainer, $config['modules']['config']['WPLoader']);
$loader->_initialize();
Yes: I'm cheating and re-using code I've already written to do the job. I call it laziness but that could go under the name of "code reuse".
Jokes aside Codeception and WPLoader are taken as granted here and used to allow for the tests to run uniformly. Parameters and instructions are in the wp-browser 1.7.0 readme file.
[](http://theaveragedev.local/wordpress/wp-content/uploads/2015/10/2015-10-05-at-21.08.png)