05: Finding untested code
Finding untested lines of code
The PHP gcov website shows what lines of C code are covered by the test suite.
Creating a new test
We made sure that we had the
ext/json extension installed.
$ sapi/cli/php -m | grep json
We tried to create a new test.
$ vi ext/json/tests/json_decode_error.phpt
But quickly realized that there was already a test there so we created a new file variation.
$ vi ext/json/tests/json_decode_error001.phpt
Then we created our test.
--TEST-- json_decode() - depth error --CREDITS-- Sammy Kaye Powers me at sammyk dot me # TestFest Chicago PHP UG 2017-07-18 --SKIPIF-- <?php if (!extension_loaded('json')) die('skip ext/json required'); ?> --FILE-- <?php var_dump(json_decode('', false, 0)); ?> --EXPECTF-- Warning: json_decode(): Depth must be greater than zero in %s on line %d NULL
We ran our test to see it pass with flying colors.
$ make test TESTS=ext/json/tests/json_decode_error001.phpt
At this point we could totally send our new test as a PR to the main php-src repo, but we wanted to see that this test actually covered the untested lines.
Generating a code coverage report
Since the PHP gcov website isn't updated regularly, we took Elizabeth Smith's advice and generated the code coverage reports locally.
First we have to install lcov.
$ sudo apt-get update $ sudo apt-get install lcov
Then we can use the handy
config.nice script to run configure again with all the previous flags in addition to any new ones. So we ran it with
--enable-gcov since we already ran it with
$ ./config.nice --enable-gcov
Next we had to delete all the previously compiled files with
make clean so that everything could be recompiled with the appropriate flags that gcov needs.
$ make clean && make
Finally we wer
Truncated by Planet PHP, read more at the original (another 1981 bytes)