Easy4PHPUnit: Easy 4 Steps PHP Unit Testing Framework

I’m fascinated a lot by the power and confidence that unit testing gives to the software developer. We use unit testing in all of my classes assignments, and I can’t imagine my life without them. I’m not a web developer, but I like to play around with different programming languages to make sure that any new concepts I learn or any new skills I acquire are transferable to any area regardless of the programming language that I use. That’s why I started writing this very basic unit testing framework in PHP just as personal educational exercise. Now I would like to share it with anyone who might find it useful and wants either to use it or learn something from it.

Note: Demo video is available at the end of this post if you prefer to watch the quick tutorial.
The GitHub repo link is available at the end of the post as well.

Framework Architecture

Easy4PHPUnit UML Diagram

Easy4PHPUnit UML Diagram

The simplicity of this framework is reflected in the simplicity of its architecture. The above UML is not meant to reflect every bit of code  in the framework, but it gives the big picture. The heart of this framework is the singleton class TestContainer, which stores a number of tests (instances of concrete derived classes from the abstract class TestCase). Those tests will eventually be run when the runAll() method is invoked, and any test failures will be added to a list of Failure instances that TestContainer manages, so that it can report those failures at the end of the test.

The abstract TestCase class has a constructor that automatically adds itself to the list of tests managed by TestContainer. So all that needs to be done is to write a concrete class that extends TestCase and implements its abstract method run(). This concrete class will represent a test case to be run, its run() method is where the unit test code should be added. The unit test code should use TestCase‘s protected methods check_equal() and check_float_equal(). Those methods will check for the equality of an expected and an actual value, and will automatically register a failure if any.

Obviously those two unit testing methods (check_equal() and check_float_equal()) are not enough, and hence TestCase needs to be extended with many more methods that do more sorts of tests, and that’s where the framework needs improvement.

If there are any failures, they will be conveniently reported with their file name(s) and line numbers as shown below:

Easy4PHPUnit Tests Results

Easy4PHPUnit Tests Results

The 4 Easy Steps

1 – Create a PHP File To Hold Your Tests

You can have multiple files as well, with each having a bunch of related test cases. In the beginning of each file require the TestIncludes.php file.

require_once 'TestIncludes.php';

2 – Create One or More Concrete Classes That Extend TestCase And Implement Its run() Method

Each concrete class represents a test case. The actual unit test code is inside your implementation of the run() method. run() must use the unit testing methods of TestCase (check_equal() and check_float_equal()) or any other similar methods you might have extended TestCase with.

class FloatingPointTest1 extends TestCase
     public function run()
         $float1 = 0.003;
         $float2 = 0.00302;
         $this->check_float_equal($float1, $float2);

3 – For Each Concrete TestCase Class You Created, Instantiate It

Instantiate the concrete classes you just created, so that the constructor of the abstract base class TestCase will add that test case to the list on the TestContainer.

$t1 = new FloatingPointTest1();

4 – Run The Tests

Go to the run.php file and require any PHP files that you created previously to hold your concrete test case classes and their instances. run.php automatically calls the runAll() method of the TestContainer class. Just execute this run.php file and the tests will be run and their results will be reported.

require_once 'TestIncludes.php';
require_once 'MyTestCase.php';

The code is well documented and is easy to follow. It is available at the following GitHub repository.

Easy4PHPUnit on GitHub

I also recorded a quick demo video to demonstrate the framework for those who like videos. Here you go:

Thank you everyone, and I hope you liked it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s