2

I am using apache2, Selenium, PHPUnit.

Some tests cause php errors but passed because it looks like everything is good from browser.

How can I catch those errors to mark test as failed?

2 Answers 2

1

The best way would be to make PHP throw ErrorExceptions on error.

set_error_handler(
    function($err_severity, $err_msg, $err_file, $err_line) { 
        throw new ErrorException($err_msg, 0, $err_severity, $err_file, $err_line) 
    }
);

Since an error is always fatal, the test will fail by default when an error occurs (note that it would halt the rest of the test if you don't assert/catch the exception).

Sign up to request clarification or add additional context in comments.

1 Comment

Error could appear at the end of page and test would pass. Also I am already use this approach :^ ).
0

PHPUnit allows to add checks that are run before or after every test function. Just add a assertPreConditions() or assertPostConditions() to your test to see if there are traces of PHP failure.

See http://phpunit.de/manual/3.7/en/fixtures.html for some more details.

Now the question is: How to detect these errors? Either check if there are error texts on the screen, or (if you run local to the apache) check if the php error log file has increased in size - it should not. This highly depends on your needs.

3 Comments

I am looking for an easier way to get errors. 1. Check error on the page -- not good solution: you must check every page, ajax responses do not display errors. Checking error.log could give user access issues. And it would be hard to get current errors only.
Or you can just use set_error_handler to trigger some sort of a flag, or throw an ErrorException.
I assume you have a dedicated test system set up. That system would be configured to testing needs. If you need errors displayed, then configure it. This will affect ajax as well. Also, you could configure access to the error log. And when you only run the test on that system, only the test will affect the logs. Apart from that, you should have some kind of unit tests as well, so PHP errors would be detected there (like notices and warnings) - on the other hand, if you get an internal warning that does not affect the website, it's not really broken, is it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.