imars world - Think about it: nobody will do it for you
Go to site navigation

Test

edited on August 6th, 2009

Tiny unit testing helper.

Summary

Introduction

Legal

Copyright (C) 2009 Stephane Lavergne http://www.imars.com/

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Description

Useful for invoking series of small tests, probably from a Makefile. The testing section of this library's Makefile follows. Note that we're using “find” rather than iterate with PHP's opendir() to make sure that a fatal error in one test file does not affect any other files. This prevents us from knowing the length of the longest file for display alignment, but I prefer function over form in this case.

test:   dep
	@find t -name '*.php' -exec php -d include_path=lib -f '{}' ';'
	@echo "*** See 'test.log' for details."

The above iterates through each “.php” file in directory “t/” and any subdirectory therein, displays a summary of results on screen and saves full debugging output in “test.log”.

Example test file

require_once 'Test.inc';

ok(function_exists('a_function_of_yours'), true);  # This is test 1

okDiff('foobar', a_function_of_yours('raboof'));  # This is test 2

ok

function ok($bool, $fatal = false)

Log a test result. If a second argument is true, this will be the last test performed in this file if it fails.

okDiff

function okDiff($left, $right, $fatal = false)

Wrapper around ok() and array_compare(). Will display any difference between arguments 1 and 2 and consider the test a success if they match perfectly. A match means that both not only represent the same value, but are also of the same type. (i.e. “2”, 2 and 2.0 are three different things.) If the arguments are arrays, they are compared properly whether they are hashes or indexed, in both directions with full recursion to find any possible differences, thanks to array_compare().

As with ok(), a if a third argument is true, this will be the last test performed in this file if it fails.

Post a comment:


(not displayed, optional)

(optional)

Don't write in these two boxes:

(URLs will become hyperlinks, please omit "http://".)

Navigation:

© 2009 Stéphane Lavergne   Site powered by FernCMS   Valid: XHTML 1.1 CSS