使用 phpunit 测试多个异常

发布于 2024-10-05 08:21:31 字数 463 浏览 0 评论 0原文

我是单元测试的新手,并编写了以下测试:

/**
 * @expectedException Exception
 */
public function testCantGetInvalidCampsite() {
    $invalidIds = array(300000, "string");
    foreach($invalidIds as $id) {
        $this->campsites->getCampsite($id); // will throw an exception
    }
}

但我不确定这是否实际上是在测试所有无效的 id,或者只是在遇到第一个异常时立即停止。这是我应该测试多个异常的方式还是我需要将其分成多个不同的测试,或者我应该有其他方法吗?

另外,如果我的异常消息是动态生成的,例如“无法检索 id 30000 的记录”,我如何测试是否正在生成正确的动态消息?

I'm new to unit testing and have written the following test:

/**
 * @expectedException Exception
 */
public function testCantGetInvalidCampsite() {
    $invalidIds = array(300000, "string");
    foreach($invalidIds as $id) {
        $this->campsites->getCampsite($id); // will throw an exception
    }
}

I'm not sure though if this is actually testing all the invalid ids, or just stopping as soon as it hits the first exception. Is this how I should be testing for multiple exceptions or do I need to split this up into a number of different tests, or is there another way I should do it?

Also, if my exception message is generated dynamically eg "Could not retrieve record with id 30000", how do I test that the correct dynamic message is being produced?

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

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

发布评论

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

评论(2

不念旧人 2024-10-12 08:21:31

我在这种情况下使用的方法是使用 phpunit dataProviders:

class MyTest extends PHPUnit_Framework_TestCase
{
    public function invalidIds()
    {
       return array(
           array(300000),
           array("string")
       );
    }


    /**
     * @dataProvider invalidIds
     * @expectedException Exception
     */
    public function testCantGetInvalidCampsite($invalidId)
    {
        $this->campsites->getCampsite($invalidId); // will throw an exception
    }
}

The approach I use in this situation is using phpunit dataProviders:

class MyTest extends PHPUnit_Framework_TestCase
{
    public function invalidIds()
    {
       return array(
           array(300000),
           array("string")
       );
    }


    /**
     * @dataProvider invalidIds
     * @expectedException Exception
     */
    public function testCantGetInvalidCampsite($invalidId)
    {
        $this->campsites->getCampsite($invalidId); // will throw an exception
    }
}
苄①跕圉湢 2024-10-12 08:21:31

您可以尝试捕获异常,对它们进行计数,如果预期异常的数量与遇到的异常的数量不匹配,则测试失败。 如果遇到异常,您

还可以获取异常消息,并检查它是否正确。

在《代码:

public function testCantGetInvalidCampsite() {
    $invalidIds = array(300000, "string");
    $exceptionCount = 0;

    foreach($invalidIds as $id) {

        try {
            $this->campsites->getCampsite($id); // will throw an exception
        } catch (Exception $e)
            $exceptionCount ++;
            $this->assertEquals('my exception message', $e->getMessage());
        }
    }

    // assert here that exception count is two
    $this->assertEquals(2, $exceptionCount);
}

更干净的方法》中,我认为要做的是添加两个测试用例......

testCantGetInvalidCampSite_String()
testCantGetInvalidCampSite_InvalidId()

这样,如果测试失败,您就可以立即看到失败的内容。

You could try to catch the Exceptions, count them and fail the Test if the number of expected Exceptions does not match the number of encountered Exceptions. Of

You could also fetch the Message of the Exception, if you encounter one, and check if it is correct.

In Code:

public function testCantGetInvalidCampsite() {
    $invalidIds = array(300000, "string");
    $exceptionCount = 0;

    foreach($invalidIds as $id) {

        try {
            $this->campsites->getCampsite($id); // will throw an exception
        } catch (Exception $e)
            $exceptionCount ++;
            $this->assertEquals('my exception message', $e->getMessage());
        }
    }

    // assert here that exception count is two
    $this->assertEquals(2, $exceptionCount);
}

The cleaner Way, in my opinion to do it would be to add two TestCases ...

testCantGetInvalidCampSite_String()
testCantGetInvalidCampSite_InvalidId()

so you see instantly what fails if the test fails.

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