6. Running PHP Extension Tests
In addition to these four files, “ext_skel” script created a “tests” directory, with several *.phpt files inside it. These are automated tests that may be executed all together, by running the “make test” command:
$ make test
…
=====================================================================
TEST RESULT SUMMARY
---------------------------------------------------------------------
Exts skipped : 0
Exts tested : 26
---------------------------------------------------------------------
Number of tests : 3 3
Tests skipped : 0 ( 0.0%) --------
Tests warned : 0 ( 0.0%) ( 0.0%)
Tests failed : 0 ( 0.0%) ( 0.0%)
Tests passed : 3 (100.0%) (100.0%)
---------------------------------------------------------------------
Time taken : 1 seconds
=====================================================================
The command will print the test execution progress and result summary. It’s a good practice to cover most extension logic with corresponding tests and always run tests after changes.
Let’s investigate the test file “tests/003.phpt.”
--TEST--
test_test2() Basic test
--SKIPIF--
<?php
if (!extension_loaded(‘test’)) {
echo ‘skip’;
}
?>
--FILE--
<?php
var_dump(test_test2());
var_dump(test_test2(‘PHP’));
?>
--EXPECT--
string(11) “Hello World”
string(9) “Hello PHP”
The file combines a few sections:
- “--TEST--” defines the test name.
- “--SKIPIF--” (optional) contains a PHP code to check skip conditions. If it prints some string, started from word “skip”, the whole test is going to be skipped. Our section prints “skip” if extension “test” is not loaded because it doesn’t make sense to check functions that are not loaded (the test would fail).
- “--FILE--” section contains the main test PHP code.
- “--EXPECT--” contains the expected output of the test script.