Part of an upcoming php.net article titled “How can you help PHP.NET today?” with this part focusing on writing tests. It's essentially in a FAQ format:
Why should I write a test?
Code coverage is important for the success of any project including PHP. When code coverage reaches 100%, it'll mean that every piece of code within the PHP sources will be executed while running the tests. This helps determine if behaviour changes so essentially it helps prevent bugs from being introduced into PHP.
What kind of setup do I need to write good tests?
gcc >= 3.4 with gcov. Your local setup should also include lcov that is available here:
Once installed, compile PHP with GCOV support using –enable-gcov so for example:
# wget foo.com/phpsrc.tar.gz # tar xfvz phpsrc.tar.gz # cd phpsrc # ./configure --with-foo --with-bar --enable-gcov # make
In summary: Install lcov, compile PHP with –enable-gcov, and now you're ready.
How do I find what needs a test?
- Go to gcov.php.net
- Choose a branch, then 'coverage'
- Will go into some detail on what it means
How do I create a test?
- Go to http://qa.php.net/…
Do you have a good example of a test and what it covers?
How do I cover uncovered code?
- Locate a file with a percentage < 100%
- Use the legend to determine uncovered code
- Write code that will execute it
How do I determine if the desired code gets executed?
- make lcov
- view lcov_html/index.html
- make a new test
- re-run lcov:
- make lcov TESTS=ext/foo/tests/bar.phpt
- Note: The TESTS var is to speed-up the process, as it will only run the specific test(s)
How and where do I submit my test?
You can submit tests to the development mailing list @ email@example.com
What is open-box and black-box testing and why do I care?
Do I need valgrind? When and why might I use it?
Short answer: You don't. Long answer: valgrind can be used to detect if some test triggers a memory-related bug in PHP. Although running the test suite with valgrind is a plus, but it takes ages. You can submit tests without running valgrind, because the http://gcov.php.net project will do that for you.
How do I install lcov on Linux, Mac, and/or Windows? What part of the package do I need?
Just untar it and run make install.