在遇到异常后,PHPUnit不会继续测试 [英] PHPUnit doesn't continue test after expecting an exception

查看:113
本文介绍了在遇到异常后,PHPUnit不会继续测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么PHPUnit不执行此代码中的最后一个异常声明?

Why doesn't PHPUnit do last exception assertion in this code?

public function testConfigOverriding()
{
    $this->dependencyContainer = new DependencyContainer(__DIR__ . "/../../Resources/valid_json.json");
    $this->assertEquals('overriden', $this->dependencyContainer->getConfig('shell_commander')['pygmentize_command']);

    $unexisting = "unexisting_file";
    $this->setExpectedException('Exception', "Configuration file at path \"$unexisting\" doesn't exist.");
    $this->dependencyContainer = new DependencyContainer($unexisting);

    $invalid = __DIR . "/../../Resources/invalid_json.json";
    $this->setExpectedException('Exception', "Configuration JSON file provided is not valid.");
    $this->dependencyContainer = new DependencyContainer($invalid);
}

所以基本上:它测试是否抛出了"unexsisting_file"异常,但完全忽略了无效的json"测试.我是否需要对引发的每个异常进行单独的测试?

So basically: it tests whether "unexsisting_file" exception was thrown, but completely ignores "invalid json" test. Do I need to make separate tests for each exception thrown?

推荐答案

即使使用setExpectedException,您的测试仍然是常规的PHP代码,并且遵循PHP的常规规则.如果引发异常,程序流将立即跳出当前上下文,直到到达try/catch块.

Even with setExpectedException, your test is still regular PHP code, and follows PHP's normal rules. If an exception is thrown, program flow immediately jumps out of the current context until it reaches a try/catch block.

在PHPUnit中,当您使用setExpectedException时,它告诉PHPUnit的核心何时应该期待将要运行的代码中的异常.因此,它将使用try/catch块等待它,并通过测试catch是否通过预期的异常类型通过了测试.

In PHPUnit, when you use setExpectedException, it tells PHPUnit's core that when it should be expecting an exception from the code that's about to run. It therefore waits for it with a try/catch block and passes the test if the catch is called with the type of exception it is expecting.

但是,在您的测试方法中,正常的PHP规则仍然适用-发生异常时,这就是当前代码块的结尾.除非您在测试方法中拥有自己的try/catch块,否则该方法将不再执行.

However, within your test method, the normal PHP rules still apply -- when the exception happens, that's the end of the current code block. Nothing more in that method will be executed, unless you have your own try/catch block within the test method.

因此,为了测试多个异常,您有几种选择:

So therefore, in order to test multiple exceptions, you have a few options:

  1. 将您自己的try/catch添加到测试方法中,以便您可以在遇到第一个异常后在该方法中进行进一步的测试.

  1. Add your own try/catch to the test method, so that you can carry on with further tests within that method after the first exception.

将测试拆分为单独的方法,以便每个异常都在其自己的测试中.

Split the tests into separate methods, so that each exception is in its own test.

这个特定的示例看起来是使用PHPUnit的dataProvider机制的一个很好的例子,因为您基本上是在用两组数据测试相同的功能. dataProvider功能允许您定义一个单独的函数,其中包含要测试的每组值的输入数据数组.然后将这些值一次一组传递到测试方法中.您的代码如下所示:

This particular example looks like a good case to use PHPUnit's dataProvider mechanism, because you're basically testing the same functionality with two sets of data. The dataProvider feature allows you to define a separate function that contains an array of input data for each set of values you want to test. These values are then passed one set at a time into the test method. Your code would look something like this:

/**
 * @dataProvider providerConfigOverriding
 */
public function testConfigOverriding($filename, $expectedExceptionText) {
    $this->dependencyContainer = new DependencyContainer(__DIR__ . "/../../Resources/valid_json.json");
    $this->assertEquals('overriden', $this->dependencyContainer->getConfig('shell_commander')['pygmentize_command']);

    $this->setExpectedException('Exception', $expectedExceptionText);
    $this->dependencyContainer = new DependencyContainer($filename);
}

public function providerConfigOverriding() {
    return array(
        array('unexisting_file', 'Configuration file at path "unexisting_file" doesn\'t exist.'),
        array(__DIR__ . "/../../Resources/invalid_json.json", "Configuration JSON file provided is not valid."),
    );
}

希望有帮助.

这篇关于在遇到异常后,PHPUnit不会继续测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆