Auto-generating test cases

Although it's not possible to generate a complete PHPT test, it's quite easy to generate the standard sections and some simple functions automatically, creating a test case "frame". To do this, you need:

Example output - a generated test case frame for the cos() function:

--TEST--
Test function cos() by calling it with its expected arguments
--FILE--
<?php


echo "*** Test by calling method or function with its expected arguments ***\n"

$number = 



var_dump(cos( $number ) );


?>
--EXPECTF--

Completing the .phpt test file:

To turn this into a complete test case, all the developer has to do is to initialise $number to something reasonable and to add a section for the expected output.

Command syntax

php generate-phpt.php  -f <function_name> |-c <class_name> -m <method_name> -b|e|v [-s skipif:ini:clean:done] [-k win|notwin|64b|not64b] [-x ext]

Options

Use -h to list them, this is the output:

-f function_name ................. Name of PHP function, eg cos
-c class name .....................Name of class, eg DOMDocument
-m method name ....................Name of method, eg createAttribute
-b ............................... Generate basic tests
-e ............................... Generate error tests
-v ............................... Generate variation tests
-s sections....................... Create optional sections, colon separated list
-k skipif key..................... Skipif option, only used if -s skipif is used.
-x extension.......................Skipif option, specify extension to check for
-h ............................... Print this message

Implementation notes

The source code is under scripts/dev/generate-phpt/src. The phar file is generated using the script gtPackage.php. The script works by using Reflection to work out what arguments a function or method expects and then setting up a function/method invocation. The catch with generating tests this way is that the script has to be run *using the level of PHP that you want to test*, so if your were trying to write tests before doing development this script will not help. However, for filling in test gaps in existing extensions it works fine.

Return to write tests.