简体   繁体   English

PHPunit-错误

[英]PHPunit - Errors

When an error that the PHPunit framework does not expect to occur happens, the testing stops, and PHP throws the error, but PHPunit does not record that it was an error. 当发生PHPunit框架不希望发生的错误时,测试将停止,PHP会抛出该错误,但是PHPunit不会记录这是一个错误。 How do I make sure PHPunit records it as an error. 如何确保PHPunit将其记录为错误。

Disclaimer, I'm new to PHPUnit and am trying to figure out the whole 'what happens when an error occurs' too. 免责声明,我是PHPUnit的新手,并且正在尝试弄清整个“发生错误时会发生什么”。

From PHPUnit's docs : PHPUnit的文档中

When the tested code contains PHP syntax errors, the TextUI test runner might exit without printing error information. 当测试的代码包含PHP语法错误时,TextUI测试运行程序可能会退出而不显示错误信息。 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. 标准测试套件加载器可以选择检查测试套件源文件是否存在PHP语法错误,但不能检查测试套件源文件包含的源文件。

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. OP所谈论的是语法或PHP错误将杀死PHPUnit。 PHP Fatal errors kill the PHP Interpreter (or cause it to halt at least), which means PHPUnit cannot proceed. PHP致命错误会杀死PHP解释器(或至少导致其停止),这意味着PHPUnit无法继续。

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). 该脚本假定脚本位于测试(./)所在的目录中,并且代码树从../开始(与常规ZendFramework 1设置类似的设置)。 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. 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. 您需要使用正常的PHP错误捕获来捕获错误,以避免崩溃,这是在PHP解释器遇到错误时发生的。

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. 我捕获了PHPUnit的输出(phpunit ...> PHPUnit.log),然后对其进行解析以从PHPUnit中查找状态(成功(已跳过/未完成,确定,失败等)...),如果未找到,则我知道PHPUnit没有完成,并且发生了错误。 The results of the error will have also been dumped to the terminal since I have my PHP configured to show errors. 错误的结果也将被转储到终端,因为我已将PHP配置为显示错误。

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. 通常,脚本会通过电子邮件发送给自己进行调查,如果我不在或无法立即进行调查,则开发团队会在电子邮件中抄送CC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM