check
PHP utility for unit testing
Function and purpose
Key methods |
Test | Does something equal something else? Eg function returns expected result. |
Is | Tests type or object class |
Trap | For investigating the throwing of exceptions |
check is a utility for structured testing.
Some people call this
unit testing. It is a framework for a few test wrappers
which is ever so easy to get started with. It is also simple enough for anyone to adjust the code
to suit their own development environment.
Examples
Two example PHP programs All in new windows
Documentation
Code outline
include('check.php');
etc.
Start block...
check::StartBlock('First block');
... some tests
check::Is('Should be array','array',$someVar); // Right sort of type
check::Test('Does it work',42,$universe->Answer()); // Expected and actual
check::Trap('Should throw exception','argument','multiply',null,'cat');
... more tests ...
check::EndBlock();
... more blocks of tests ...
check::Finish();
Getting started
- Installation
- Download package from vulpeculox.net. DOWNLOAD NOW
- Unzip somewhere
- Run the example .php files to see check in operation
- Move check.php and dump.php into your PHP include path.
Run the example programs again to test.
- Familiarisation
- Have a browse of the comments in demoCheck.php
- Remove the commenting from the line marked with [1]
... The clean block will only show its summary.
- Remove the commenting from the line marked with [2]
... The screen will not show the results but they will be sent to a file.
- If you remove the comments from the line marked [3]
something horrible happens. We are trying to multiply an array which PHP
treats as a fatal error. Handling this situation requires thinking about.
Reference
Calling check::Help() displays the reference.
See it now
|
- Notes
- Splitting tests into blocks is de-rigeur. This gives you a structure and makes de-cluttering
your results a lot easier.
- In the examples I have used a reference style of labelling. You can do whatever suits you
from simple numbers to full descriptions.
- Normally output is buffered. Sometimes you want to see what else is going on with
checks interleaved with other output. Disable buffering for a block with a second argument to
check::StartBlock().
- check::TestNot() is a funny thing. Use this when you know of a
possible rogue result and want it reported if it happens.
- You can of course put tests in loops. In demoCheck.php there is an
example of how the verbosity can be tweaked to de-clutter the results.
- When trapping on a function that takes a single array as its argument you would
call it as follows: check::Trap('T1','empty','maxOfArray',null,array(array(1,2,3)))
(note the nested array). An un-nested array here would supply a number of arguments
not a single argument.
|