PHPunit - Errors
When an error that the PHPunit framework does not expect to occur happens, the testing stops, and PHP throws the error, but PHPuni开发者_运维百科t does not record that it was an error. How do I make sure PHPunit records it as an error.
Disclaimer, I'm new to PHPUnit and am trying to figure out the whole 'what happens when an error occurs' too.
From PHPUnit's docs:
When the tested code contains PHP syntax errors, the TextUI test runner might exit without printing error information. The standard test suite loader can optionally check the test suite sourcefile for PHP syntax errors, but not sourcefiles included by the test suite sourcefile.
And the option:
--syntax-check Try to check source files for syntax errors.
What the OP is talking about is a syntax or PHP error will kill PHPUnit. PHP Fatal errors kill the PHP Interpreter (or cause it to halt at least), which means PHPUnit cannot proceed.
If you really wanted to avoid this case you could add some of the following bits in a script. This script assumes the script is in the directory with your tests (./) and that your code tree starts at ../ (similar setup to a normal ZendFramework 1 setup). Dont bother with using code coverage with this script, it will only be correct for the last run unittest:
#!/bin/bash
for i in $(find ../ -name "*.php"); do
msg=`php -l $i`
if [ "$?" != "0" ]; then
echo $msg;
fi
done
for i in $(find ./ -name "*Test.php"); do
echo "Running Test: $i";
phpunit $i
done
HTH.
You would need to capture the error using the normal PHP Error capture to avoid the crash to the OS, which happens when the PHP Interpreter encounters the error.
Automated tests should be checked and tested before being committed to your main development stream.
I capture the output from PHPUnit (phpunit ... > PHPUnit.log) which I then parse looking for the status from PHPUnit (Success with Skipped/Incomplete, OK, FAILURE, etc...) and if this is not found, then I know PHPUnit did not complete, and an error occurred. The results of the error will have also been dumped to the terminal since I have my PHP configured to show errors.
On an error, I simply mail this log file to the development team, or the person who last changed the test file, to investigate. Logic for the email may get complex if the scripts try to decide the root cause/person to send the mail to. Usually, the script emails myself to investigate, and the development team is CC'd on the email in case I am away or unavailable to investigate right away.
精彩评论