PHPunit 忽略设置方法中的异常

发布于 2024-11-19 06:20:21 字数 581 浏览 3 评论 0 原文

我注意到 PHPUnit 会忽略 setUp() 方法中引发的异常,并且即使在 setup 函数引发异常时也只是运行测试。

在下面的代码中,异常将被忽略,下面的代码将不会运行,并且 test_method 将失败,因为它使用了未定义的变量。

protected $a;

public function setUp() {
    parent:setUp();
    throw new Exception(); // setup now exits silently.
    $this->a = new A(); // will never run
}

public function testA() {
    $this->assertTrue($this->a->something()); // will exit tests with PHP error, because $this->a === null
}

我通过 CLI 使用 phpunit.xml 配置文件运行 phpunit。

有谁知道如何让 PHPunit 报告异常,然后停止 testCase 的执行?

I noticed that PHPUnit ignores exceptions thrown in the setUp() method, and simply runs tests even when the setup function throws an exception.

In the below code the exception will be ignored, the code below it will not run, and the test_method will fail because it is using an undefined variable.

protected $a;

public function setUp() {
    parent:setUp();
    throw new Exception(); // setup now exits silently.
    $this->a = new A(); // will never run
}

public function testA() {
    $this->assertTrue($this->a->something()); // will exit tests with PHP error, because $this->a === null
}

I'm running phpunit through the CLI with an phpunit.xml configuration file.

Does anyone know a way to make PHPunit report on the exception, and then stop the execution of the testCase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-11-26 06:20:21

在这里抛出异常不是正确的方法,PHPUnit中有一个特殊的方法:

<?php
class DatabaseTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        if (!extension_loaded('mysqli')) {
            $this->markTestSkipped(
              'The MySQLi extension is not available.'
            );
        }
    }

    public function testConnection()
    {
        // ...
    }
}
?>

http://www.phpunit.de/manual/current/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests

Throwing Exception is not the right way to do here, there is a special method in PHPUnit:

<?php
class DatabaseTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        if (!extension_loaded('mysqli')) {
            $this->markTestSkipped(
              'The MySQLi extension is not available.'
            );
        }
    }

    public function testConnection()
    {
        // ...
    }
}
?>

http://www.phpunit.de/manual/current/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests

遗失的美好 2024-11-26 06:20:21

无法重现

运行脚本(下面的完整示例)会产生带有异常的错误输出。

我假设你在其他地方有问题或者可能是旧的 phpunit 版本?即便如此,我也不知道那段代码有任何变化。

可能也从主干运行phpunit? (“3.6”)在这种情况下,“Exception”类的处理其自身发生了变化,现在无法测试这种情况,但如果这适用于您,请尝试使用 InvalidArgumentException() (仅用于测试)并看看这是否会改变事情。

phpunit test.php
PHPUnit 3.5.13 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 3.00Mb

There was 1 error:

1) FooTest::testA
Exception: hi

/home/.../test.php:10

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

您的代码可运行:

<?php

class FooTest extends PHPUnit_Framework_TestCase {


    protected $a;

    public function setUp(){
        parent::setUp();
        throw new Exception('hi'); //setup now exits silently.
        $this->a = new A(); //will never run
    }

    public function testA(){
        $this->assertTrue($this->a->something()); //will exit tests with PHP error, because $this->a === null
    }

}

Can't repoduce

Running the script (full example below) produces an error output with the exception.

I'd assume you have a problem elsewhere or maybe a old phpunit version? Even so I'm not aware of any changes in that piece of code.

You might also be running phpunit from trunk? ("3.6") In that case the handling of the "Exception" class its self changed, can't test that case right now but if that applies to you try using a InvalidArgumentException() (just for testing) and see if that changes things.

phpunit test.php
PHPUnit 3.5.13 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 3.00Mb

There was 1 error:

1) FooTest::testA
Exception: hi

/home/.../test.php:10

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

Your Code made runable:

<?php

class FooTest extends PHPUnit_Framework_TestCase {


    protected $a;

    public function setUp(){
        parent::setUp();
        throw new Exception('hi'); //setup now exits silently.
        $this->a = new A(); //will never run
    }

    public function testA(){
        $this->assertTrue($this->a->something()); //will exit tests with PHP error, because $this->a === null
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文