PHPUnit 中的expectOutputString 或expectOutputRegex

发布于 2024-12-22 12:45:04 字数 595 浏览 4 评论 0原文

您好,我正在使用 PHPUnit 进行单元测试,

我在使用 ExpectOutputString/expectOutputRegex 方法测试输出时遇到问题

问题:

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz');
    $this->expectOutputString('test');
    echo 'test';
}

当我生成单元测试报告时,此测试通过,即使 第一个期望失败

与断言方法不同,如果有一个断言失败,则测试失败

示例assertTrue:

// this test fail because the first assertTrue fails
function test_myAssert() {
  $this->assertTrue(false);
  $this->assertTrue(true);
}

看起来这是PHPUnit中缺乏的功能..

有人有想法或替代方法来实现我在测试输出时想要的功能吗?

Hi I am using PHPUnit for my unit testing

I have a problem regarding testing output using the expectOutputString/expectOutputRegex method

Problem :

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz');
    $this->expectOutputString('test');
    echo 'test';
}

This test pass when i generate unit test report even though the
first expectation fails

Unlike in the assert methods, test fail if there is one assertion that fails

Example assertTrue :

// this test fail because the first assertTrue fails
function test_myAssert() {
  $this->assertTrue(false);
  $this->assertTrue(true);
}

Looks like this is a lacking functionality in PHPUnit..

Anyone have ideas or alternative way to achieve what I want when testing output?

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

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

发布评论

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

评论(2

念﹏祤嫣 2024-12-29 12:45:04

expectOutputString 存储给定的字符串以与整个测试的输出进行比较,但它会覆盖任何先前存储的字符串。换句话说,只有最后一次调用 expectOutputString 才会产生任何效果。您必须构建完整的输出字符串并仅调用一次expectOutputString

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz' . 'test');
    echo 'test';
}

上面的代码将会失败,因为 testxzxzxzxzxztest 不等于输出 test

expectOutputString stores the given string to compare against the output of the whole test, but it overwrites any previously stored string. In other words, only the last call to expectOutputString has any effect. You must build the full output string and call expectOutputString just once.

function test_myTest() {
    $this->expectOutputString('testxzxzxzxzxz' . 'test');
    echo 'test';
}

The above will fail because testxzxzxzxzxztest does not equal the output test.

说好的呢 2024-12-29 12:45:04

expectOutputString 测试整个输出,因此多次调用它并没有真正意义。但是,多次调用 expectOutputRegex 是有意义的,因为一个字符串可以匹配多个正则表达式。然而,PHPUnit 只记住最后一个要匹配的正则表达式。因此,对 expectOutputRegex 的后续调用会默默地覆盖预期值。

我创建了以下支持多次调用 expectOutputRegex 的类:

abstract class MyTestCase {
    private array $expectedRegexes = [];

    protected function tearDown() : void {
        $this->assertExpectedRegexes();
    }

    public function expectOutputRegex(string $expectedRegex) : void {
        parent::expectOutputRegex($expectedRegex);
        $this->expectedRegexes[] = $expectedRegex;
    }

    private function assertExpectedRegexes() {
        $output = $this->getActualOutput();
        foreach ($this->expectedRegexes as $regex) {
            $this->assertMatchesRegularExpression($regex, $output);
        }
    }
}

expectOutputString tests the whole output, so calling it multiple times does not really makes sense. However, calling expectOutputRegex multiple times would make sense, since a string can match multiple regular expressions. However, PHPUnit remembers only the last regular expression to match against. So subsequent calls to expectOutputRegex silently overwrite the expected value.

I created the following class that supports calling expectOutputRegex multiple times:

abstract class MyTestCase {
    private array $expectedRegexes = [];

    protected function tearDown() : void {
        $this->assertExpectedRegexes();
    }

    public function expectOutputRegex(string $expectedRegex) : void {
        parent::expectOutputRegex($expectedRegex);
        $this->expectedRegexes[] = $expectedRegex;
    }

    private function assertExpectedRegexes() {
        $output = $this->getActualOutput();
        foreach ($this->expectedRegexes as $regex) {
            $this->assertMatchesRegularExpression($regex, $output);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文