03: All about .phpt files
Now that we know how to run the test suite with run-tests, let's create our first .phpt
file.
Create a basic test
We started by creating a basic test file that tested that echo
could take a list of arguments. We created a new file in the root php-src
directory called echo_basic.phpt
.
$ vi echo_basic.phpt
--TEST--
echo - basic test for echo language construct
--FILE--
<?php
echo 'This works ', 'and takes args!';
?>
--EXPECT--
This works and takes args!
Then we ran the basic test with make.
$ make test TESTS=echo_basic.phpt
We talked about the section names a bit.
-
--TEST--
A very brief description of the test; if you need more room use the--DESCRIPTION--
section -
--FILE--
The PHP code we want to test (make sure to include the closing?>
) -
--EXPECT--
The literal output that the PHP code should generate
Testing non-deterministic output
We learned that --EXPECT--
isn't the best section to use when our test generats non-deterministic output. We created an error test for the filemtime()
function to illustrate this. We tried to set the --EXPECTF--
section to the following warning.
Warning: filemtime() expects exactly 1 parameter, 0 given in /usr/src/php-src/filemtime_error.phpt on line 5
But we ran into the problem where the line number and file name in the output would change so we used --EXPECTF--
instead so that we could insert substitution characters.
$ vi filemtime_error.phpt
Note: This contrived example is essentially just testing that ZPP works (which is already very well tested) so this isn't a real-world test you'd want to create.
--TEST--
filemtime() - test error case
--FILE--
<?php
echo filemtime();
?>
--EXPECTF--
Warning: filemtime() expects exactly 1 parameter, 0 given in %s on line %d
Skipping tests
We discovered that there are situations where tests weren't able to run. This issue came up when we tried to create a test for the curl_init()
function but we didn't have the curl extension installed. We implemented the --SKIPIF--
section to alleviate this.
$ vi curl_init_basic.phpt
--TEST--
curl_init() - creates a resource
--SKIPIF--
<?php if(!extension_loaded('curl')) die('skip ext/curl required'); ?>
--FILE--
<?php
$ch = curl_init();
var_dump(is_resource($ch));
curl_close($ch
Truncated by Planet PHP, read more at the original (another 2997 bytes)