WordPress loader for functional testing – 02
August 22, 2014
Following up from my previous post I'm trying to integrate WordPress automated testing suite and Codeception.
The basics
After following the installation guide I was able to run WordPress tests from the command line connecting to a Vagrant hosted database used for testing.
Once sure the test suite works I've tried writing a test of mine; I've downloaded the WordPress development release and taking a look around (the /phpunit.xml.dist
file) reveals test will live in the /tests/phpunit/tests
directory and that, aside for report settings and test suites definition, the file that will load WordPress is the /tests/phpunit/includes/boostrap.php
file and its first doc line says it all:
/**
* Installs WordPress for running the tests and loads WordPress and the test libraries
*/
A simple test
I want to make a simple test work and have removed all of WordPress test from the /tests/phpunit/tests
folder, run the tests again and made sure no test did run [](http://theaveragedev.local/wordpress/wp-content/uploads/2014/08/2014-08-22-at-07.59.png) Knowing that I've created a simple test relying on the WordPress defined
wp_strip_all_tags
function in the /tests/phpunit/tests
folder:
<?php
/**
* FirstTest
*
* @group user
*/
class FirstTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* it should strip all tags
*/
public function it_should_strip_all_tags()
{
$in = '<p>some</p>';
$expected = 'some';
$this->assertEquals($expected, wp_strip_all_tags($in));
}
}
and running phpunit
again will result in a passing test [](http://theaveragedev.local/wordpress/wp-content/uploads/2014/08/2014-08-22-at-08.46.png)
Codeception
Since the tests work thanks to the bootstrap.php
file and the settings that it loads I will try a proof-of-concept integration in Codeception creating a suite and adding tests to it; at the command line, in my current plugin project root folder, I create a new test suite
codecept generate:suite wordpress
and will point its bootstrap file to the one defined in the the wordpress folder. I've checked out the WordPress folder on my desktop, in the ~/Desktop/wordpress-develop
folder, and will leave the bootstrap.php
file in its original position for the time being.
In the /tests/wordpress/bootstrap.php
file of my project I will include the WordPress unit test bootstrap file like
<?php
$wpBootstrapFilePath = getenv('HOME') . '/Desktop/wordpress-develop/tests/phpunit/include/bootstrap.php';
include_once $wpBootstrapFilePath;
I will replicate the phpunit
test above in the suite like
codecept generate:phpunit wordpress First
and run it
codecept run wordpress
It all seems to work as expected [](http://theaveragedev.local/wordpress/wp-content/uploads/2014/08/2014-08-22-at-15.12.png)
Next
I will set up some more testing and will then move into an organic Codeception integration of the suite to skip as much configuration as possible.