PHP Implementation of Test::More

by Mike Willbanks on April 7th, 2006

Lately I have been looking at the PHP Test::More implementation that Chris Shiflett has been writing. I must say it is extremely easy compared to testing with the larger testing frameworks. However I do believe it has it’s drawbacks which I will get to a little later. Test::More is actually a perl implementation brought to us by Apache. It is an amazingly simple testing tool for your code that doesn’t take you such an incredible amount of time to run a test on everything. Which I guess that is fantastic news to us developers.

Testing is one of those things that absolutely needs to be done. With out it you may run into logic errors and syntax errors that may be hidden. Allowing yourself to test what certain variables should be allows you to significantly lower those types of issues. These types of tests do not help you with writing secure applications, ensuring code quality nor anything else that is around that area. In essence it just allows you to test that all of your code “works”.

What I like about Test::More is that I can test my applications very quickly rather than writing huge unit tests that take up essential time in getting a project done on time and under budget. I also like that the syntax is extremely easy to write and understand quickly if another developer was to ever encounter the test cases. It also implements TAP (Test Anything Protocol). Which makes it very nice to move on to a different testing framework that might encorporate it in the future.

What I did not like about the PHP implementation of Test::More was that there is no function to iterate through an array. Since you would probably be checking the same items in that array, you may only want to output the pass or failure one time instead of multiples. Meanwhile I see why because this starts to take it away from the KISS (Keep It Simple Stupid) way. Next was that it did not prefix variables or prefix functions. Sometimes some software projects use bad naming and I feel this might cause a conflict. Instead of the developer of that project adapting and changing how their application works they will more than likely drop the tester and continue on. Last but not least is that I feel output could infact use varables as a means of an output template for each specific item. This would allow for more flexibility on how the output could be rendered. Allowing developers and/or companies to integrate it tighter into their testing needs.

What would a post be without an actual example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
include('test-more.php');
 
class preUser {
    private $_id;
    public function getId() {
        if ($this->_id = (int) $this->_id) {
            return $this->_id;
        }
        return false;
    }
    public function setId($id) {
        if ($id = (int) $id) {
            $this->_id = $id;
            return true;
        }
        return false;
    }
}
 
plan(2);
 
$user = new preUser;
ok($user->setId(1), 'Testing setId()');
is($user->getId(), 1, 'Testing getId() matches what we set');

This outputs:


1..2
ok 1 - Testing setId()
ok 2 - Testing getId() matches what we set

From PHP

2 Comments
  1. “What I did not like about the PHP implementation of Test::More was that there is no function to iterate through an array.”

    There is actually a function called is_deeply() that Perl folks use for this. I just need to implement it in the PHP version in a way that makes sense to us. It is one of the few outstanding TODO items.

    I’ve considered contributing a Test::More class to PEAR or the Zend Framework or some other library of components, and that would eliminate the concern over naming collisions. For now, I just implemented it to be as close as possible to the real Test::More. It has worked very well for Perl folks, so I think it might work for us, too.

    Thanks for checking it out. :-)

  2. a function called is_deeply() that Perl folks use for this.

    I must have missed that one when I was looking through the different implementations. I started trying to implement something that would iterate through the array but then I took something that should be simple and started to make it unnessesarily complex. That was when I did the smart thing and walked away!

    I’ve considered contributing a Test::More class to PEAR or the Zend Framework or some other library of components

    That sounds like a very good idea!

    What I was saying about the actual variables was that a string could be used as an output container that could easily be overwritten (if in a class) where we could actually specify almost like a template to manipulate the output. If that happens although we start dealing with more design patterns and taking the simplistic approach out of it.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS