Unit Testing for WordPress
Harshad Mane
PHP Developer
WordPress Enthusiast

harshadmane.in

@harshadmane
No one writes a PERFECT CODE :)
Software Testing?
It’s impossible to guarantee a bug-free release, but you
can minimize the chances by using automated tests.
There’s nothing better than ending your day with a
clean conscience knowing you did your job right.
Test cases are executed manually without any support
from tools or scripts.
Manual Testing
Test cases are executed with the assistance of tools,
scripts, and software.
Automated Testing
Manual/Automated Testing: Pros and Cons
Manual Automated
Not accurate. More reliable
Time Consuming Process Faster than a manual approach.
It does not involve in programming
task to fetch hidden information.
Testers can test complicated
application too.
Useful in UI Testing Not always!
Automated software testing can increase the depth and
scope of tests to help improve software quality.
Why do we need automation testing?
Lengthy tests that are often avoided during manual
testing can be run unattended.
A unit test is a piece of code that exercises another
piece of code.
What are Unit Tests?
A unit test should check that one piece of code, such as
a function or method, runs properly and returns the
expected results.
How to setup unit tests for your code.
We will walk through
How to write the unit tests.
How to run the tests.
How to write a testable code.
Let’s look quickly at a simple example
function hm_get_vat( $quantity )
{
return $quantity * 0.15;
}
A good unit test for our hm_get_vat() function would try
passing in various numbers in various formats to
determine if the results are always what we expect them
to be.
A unit test is nothing more than a programmatic check
to determine if a chunk of code performs the way it is
supposed to.
public function test_hm_get_vat()
{
$obj = new Class;
$vat = $obj->hm_get_vat(100);
$this->assertEquals( 15, $vat );
}
Setting up the Testing Suite
Easiest way to setup Unit Tests is through WP-CLI
WP-CLI, a command line interface for WordPress.
http://wp-cli.org/
https://make.wordpress.org/cli/handbook/installing/
1. Install PHPUnit
https://github.com/sebastianbergmann/
phpunit#installation
2. Install WP-CLI
https://make.wordpress.org/cli/handbook/plugin-unit-
tests/
1. Go to your WordPress install root folder
cd /Applications/MAMP/htdocs/phpunit/
2. Instruct WP-CLI to create the initial unit test files
wp scaffold plugin-tests <plugin-slug>
This will generate all of the files needed for our unit
tests.
wp scaffold child-theme Generate child theme based on an existing theme.
wp scaffold plugin Generate starter code for a plugin.
wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin.
wp scaffold post-type Generate PHP code for registering a custom post type.
wp scaffold taxonomy Generate PHP code for registering a custom taxonomy.
wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme.
wp scaffold _s Generate starter code for a theme based on _s.
https://developer.wordpress.org/cli/commands/scaffold/
The new folders / files created:
bin/
install-wp-tests.sh
tests/
bootstrap.php
test-sample.php
phpunit.xml
.travis.yml
These files are the foundation of our plugin’s
test suite.
bash bin/install-wp-tests.sh phpunit_test root
root localhost latest
Above command will create a test database and install
all test dependencies.
The testing suite that WP-CLI sets up for us includes
one sample unit test
It is located in tests/test-sample.php:
class SampleTest extends WP_UnitTestCase {
function testSample() {
// replace this with some actual testing code
$this->assertTrue( true );
}
}
Writing your First Test
phpunit.xml
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php"d>./tests/
</directory>
</testsuite>
</testsuites>
</phpunit>
This tells PHPUnit where to look for the PHP file
tests/bootstrap.php defines a few options, and then
also tells PHPUnit where the actual unit tests live
Here files which are prefixed with “test-“ are only
considered
Note: only methods prefixed with “test” will be
considered a unit test. All other methods will be
skipped.
Things to note down while writing tests
Separate the tests into multiple files so that they are
logically organized
Every class contains an extension of the
WP_UnitTestCase class.
The name of the class does not really matter, just name
it something that makes sense.
Assertions
An assertion is one “check” within the the test to
determine if a value is equal to what we expect.
Tests
Group of one or more assertions are Tests
One test with a single assertion:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017';
$this->assertEquals( 'WordCamp Kathmandu 2017',
$string );
}
One test with a two assertions:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017 - Awesome Event‘;
$this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome
Event', $string );
$this->assertNotEquals( 'WordCamp Kathmandu 2017',
$string );
}
https://phpunit.de/manual/current/en/appendixes.assertions.html
Q&A
Thank you!
धन्यवाद :)

Unit testing for WordPress

  • 2.
  • 3.
    Harshad Mane PHP Developer WordPressEnthusiast
 harshadmane.in
 @harshadmane
  • 4.
    No one writesa PERFECT CODE :) Software Testing? It’s impossible to guarantee a bug-free release, but you can minimize the chances by using automated tests. There’s nothing better than ending your day with a clean conscience knowing you did your job right.
  • 5.
    Test cases areexecuted manually without any support from tools or scripts. Manual Testing
  • 6.
    Test cases areexecuted with the assistance of tools, scripts, and software. Automated Testing
  • 7.
    Manual/Automated Testing: Prosand Cons Manual Automated Not accurate. More reliable Time Consuming Process Faster than a manual approach. It does not involve in programming task to fetch hidden information. Testers can test complicated application too. Useful in UI Testing Not always!
  • 8.
    Automated software testingcan increase the depth and scope of tests to help improve software quality. Why do we need automation testing? Lengthy tests that are often avoided during manual testing can be run unattended.
  • 9.
    A unit testis a piece of code that exercises another piece of code. What are Unit Tests? A unit test should check that one piece of code, such as a function or method, runs properly and returns the expected results.
  • 10.
    How to setupunit tests for your code. We will walk through How to write the unit tests. How to run the tests. How to write a testable code.
  • 11.
    Let’s look quicklyat a simple example
  • 12.
    function hm_get_vat( $quantity) { return $quantity * 0.15; }
  • 13.
    A good unittest for our hm_get_vat() function would try passing in various numbers in various formats to determine if the results are always what we expect them to be.
  • 14.
    A unit testis nothing more than a programmatic check to determine if a chunk of code performs the way it is supposed to.
  • 15.
    public function test_hm_get_vat() { $obj= new Class; $vat = $obj->hm_get_vat(100); $this->assertEquals( 15, $vat ); }
  • 16.
    Setting up theTesting Suite
  • 17.
    Easiest way tosetup Unit Tests is through WP-CLI WP-CLI, a command line interface for WordPress. http://wp-cli.org/ https://make.wordpress.org/cli/handbook/installing/
  • 18.
    1. Install PHPUnit https://github.com/sebastianbergmann/ phpunit#installation 2.Install WP-CLI https://make.wordpress.org/cli/handbook/plugin-unit- tests/
  • 19.
    1. Go toyour WordPress install root folder cd /Applications/MAMP/htdocs/phpunit/ 2. Instruct WP-CLI to create the initial unit test files wp scaffold plugin-tests <plugin-slug> This will generate all of the files needed for our unit tests.
  • 20.
    wp scaffold child-themeGenerate child theme based on an existing theme. wp scaffold plugin Generate starter code for a plugin. wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin. wp scaffold post-type Generate PHP code for registering a custom post type. wp scaffold taxonomy Generate PHP code for registering a custom taxonomy. wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme. wp scaffold _s Generate starter code for a theme based on _s. https://developer.wordpress.org/cli/commands/scaffold/
  • 21.
    The new folders/ files created: bin/ install-wp-tests.sh tests/ bootstrap.php test-sample.php phpunit.xml .travis.yml These files are the foundation of our plugin’s test suite.
  • 22.
    bash bin/install-wp-tests.sh phpunit_testroot root localhost latest Above command will create a test database and install all test dependencies.
  • 23.
    The testing suitethat WP-CLI sets up for us includes one sample unit test It is located in tests/test-sample.php: class SampleTest extends WP_UnitTestCase { function testSample() { // replace this with some actual testing code $this->assertTrue( true ); } }
  • 24.
  • 25.
  • 26.
    This tells PHPUnitwhere to look for the PHP file tests/bootstrap.php defines a few options, and then also tells PHPUnit where the actual unit tests live Here files which are prefixed with “test-“ are only considered Note: only methods prefixed with “test” will be considered a unit test. All other methods will be skipped.
  • 27.
    Things to notedown while writing tests Separate the tests into multiple files so that they are logically organized Every class contains an extension of the WP_UnitTestCase class. The name of the class does not really matter, just name it something that makes sense.
  • 28.
    Assertions An assertion isone “check” within the the test to determine if a value is equal to what we expect. Tests Group of one or more assertions are Tests
  • 29.
    One test witha single assertion: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017'; $this->assertEquals( 'WordCamp Kathmandu 2017', $string ); } One test with a two assertions: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017 - Awesome Event‘; $this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome Event', $string ); $this->assertNotEquals( 'WordCamp Kathmandu 2017', $string ); } https://phpunit.de/manual/current/en/appendixes.assertions.html
  • 30.
  • 31.