PHPUnit 通过配置中没有断言的测试

发布于 2024-11-19 13:28:34 字数 178 浏览 4 评论 0原文

我想通过获得以下内容的测试:“此测试没有执行任何断言”

我知道我可以添加类似 assertTrue(true) 的内容,但是是否可以在配置中添加一些内容来使这些测试通过得更干净吗?

我很确定这只发生在 PHPUnit 3.5.0 版本之后,引入了 --严格

I want to pass tests which get the following: "This test did not perform any assertions"

I know I could add something like assertTrue(true) however is it possible to add something to the config to make these tests pass cleaner?

I'm pretty sure this only happens since version PHPUnit 3.5.0 with the introduction of
--strict

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

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

发布评论

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

评论(8

久随 2024-11-26 13:28:34

编辑:您有几种选择,具体取决于您使用的版本、是否要忽略所有有风险的测试或只是少数,以及是否需要是永久的还是临时的。

在 5.6 之前,如果您不想在所有测试中添加虚假断言,则必须避免将 --strict 传递给 PHPUnit 或添加 strict="false"到您的 phpunit.xml。此选项的要点是“如果未做出断言,则将测试标记为不完整”。

在某些时候,PHPUnit 添加了相关的 --dont-report-useless-tests 命令行开关和 beStrictAboutTestsThatDoNotTestAnything="false" 配置选项。我还没有检查它们是否是替代品或额外的细粒度版本。

上述选项影响所有有风险的测试。使用它们会让您意外地编写没有断言的测试。以下新选项更安全,因为您必须有目的地标记您希望允许的每个有风险的测试。

PHPUnit 5.6 添加了 @doesNotPerformAssertions 注释将单个测试用例标记为“没有风险”,即使他们没有做出任何断言。

/**
 * @doesNotPerformAssertions
 */
public function testWithoutAsserting() {
    $x = 5;
}

PHPUnit 7.2 引入了TestCase::expectNotToPerformAssertions() 其中做同样的事情。

public function testWithoutAsserting() {
    $this->expectNotToPerformAssertions();
    $x = 5;
}

Edit: You've got a few choices depending on which version you're using, whether you want to ignore all risky tests or just a few, and if you want it to be permanent or temporary.

Prior to 5.6, if you didn't want to add bogus assertions to all your tests, you had to avoid passing --strict to PHPUnit or add strict="false" to your phpunit.xml. The point of this option is to "Mark a test as incomplete if no assertions are made."

At some point, PHPUnit added the related --dont-report-useless-tests command-line switch and beStrictAboutTestsThatDoNotTestAnything="false" config option. I haven't checked if they are replacements or additional fine-grained versions.

The above options affect all risky tests. Using them will leave you open to accidentally writing tests without assertions. The following new options are safer as you have to purposefully mark each risky test that you wish to allow.

PHPUnit 5.6 added the @doesNotPerformAssertions annotation to mark individual test cases as "not risky" even though they perform no assertions.

/**
 * @doesNotPerformAssertions
 */
public function testWithoutAsserting() {
    $x = 5;
}

PHPUnit 7.2 introduced TestCase::expectNotToPerformAssertions() which does the same thing.

public function testWithoutAsserting() {
    $this->expectNotToPerformAssertions();
    $x = 5;
}
瑾兮 2024-11-26 13:28:34

使用 PHPUnit 7.2,您可以获得另一种选择:

public function testCodeWithoutUsingAssertions()
{
    $this->expectNotToPerformAssertions();
    // do stuff...
}

另请参阅 https://github.com/sebastianbergmann/phpunit/pull /3042

With PHPUnit 7.2 you get another alternative:

public function testCodeWithoutUsingAssertions()
{
    $this->expectNotToPerformAssertions();
    // do stuff...
}

See also https://github.com/sebastianbergmann/phpunit/pull/3042.

若沐 2024-11-26 13:28:34

使用 @doesNotPerformAssertions 注解

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{
    // do stuff...
}

Use the @doesNotPerformAssertions annotation:

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{
    // do stuff...
}
℡寂寞咖啡 2024-11-26 13:28:34

使用$this->addToAssertionCount(1)。见下文。

class NoAssertTest extends PHPUnit_Framework_TestCase
{
    function testWithoutAssertions() {
        $x = 5;

        // Increment the assertion count to signal this test passed.
        // This is important if you use a @depends on this test
        $this->addToAssertionCount(1);
    }
}

Make use of $this->addToAssertionCount(1). See below.

class NoAssertTest extends PHPUnit_Framework_TestCase
{
    function testWithoutAssertions() {
        $x = 5;

        // Increment the assertion count to signal this test passed.
        // This is important if you use a @depends on this test
        $this->addToAssertionCount(1);
    }
}
信愁 2024-11-26 13:28:34

就我而言,测试没有断言,因为我只是使用 prophecy lib 检查所有内容来模拟并检查方法是否按预期调用...

PHPUnit 默认情况下期望测试至少有一个断言,并警告你这样说。

只需在测试的任何部分添加这一行就足够了。

$this->expectNotToPerformAssertions();

但你也可以设计一个测试,在某些情况下有断言或没有断言

public function testCodeNoAssertions()
{
    if ($something === true) {
        $this->expectNotToPerformAssertions();
    }
    // do stuff...

    if ($somethingElse === true) {
    //do any assertion
    }
}

总之,这不是一个错误......这是一个功能。

In my case, the test has no assertion because I'm just checking everything using prophecy lib to mock and check that methods are being called as expected...

PHPUnit by default expects that test has at least one assertion, and warn you to put it.

Just adding this line in any part of your test is enough.

$this->expectNotToPerformAssertions();

But also you can design a test that in some cases have assertions or not

public function testCodeNoAssertions()
{
    if ($something === true) {
        $this->expectNotToPerformAssertions();
    }
    // do stuff...

    if ($somethingElse === true) {
    //do any assertion
    }
}

In conclusion, It's not a bug... It's a feature.

Hello爱情风 2024-11-26 13:28:34

如果您有 PHP SimpleTest,另一种方法是使用:

$this->pass();

这会将测试标记为已完成并通过。

另一方面,对于您想要失败的测试,您可以使用:

$this->fail();

例如:

if (someComplicatedLogicValidation) {
    // Do more stuff and asserts
} else{
    $this->fail();
}

我在 PHP 5.5 中尝试了这个并且有效:

function testThatWorks() {
    $this->pass();
}

function testThatFails() {
    $this->fail();
}

输出:

1) 在 [...ExampleTest.unit.php 第 23 行] 处失败
在测试失败中
在示例测试中
在.../ExampleTest.unit.php
3.02秒后失败

也许方法传递在 PHPUnit 中仍然可以轻松实现。来源来自SimpleTest:

function pass($message = "Pass") {
    if (! isset($this->reporter)) {
        trigger_error('Can only make assertions within test methods');
    }
    $this->reporter->paintPass(
    $message . $this->getAssertionLine());
    return true;
}

If you have PHP SimpleTest, another approach would be to use:

$this->pass();

This will mark the test as completed and passed.

On the other hand, for a test that you want to fail, you can use:

$this->fail();

For example:

if (someComplicatedLogicValidation) {
    // Do more stuff and asserts
} else{
    $this->fail();
}

I tried this in PHP 5.5 and works:

function testThatWorks() {
    $this->pass();
}

function testThatFails() {
    $this->fail();
}

Output:

1) Fail at [...ExampleTest.unit.php line 23]
in testThatFails
in ExampleTest
in .../ExampleTest.unit.php
fail in 3.02s

Maybe the method pass can still be easily implemented in PHPUnit. Source from SimpleTest:

function pass($message = "Pass") {
    if (! isset($this->reporter)) {
        trigger_error('Can only make assertions within test methods');
    }
    $this->reporter->paintPass(
    $message . $this->getAssertionLine());
    return true;
}
不即不离 2024-11-26 13:28:34

PHPUnit 7.2+。如果您有条件,可以使用 $this->expectNotToPerformAssertions() ,这可能会禁用断言。似乎不在官方文档中,但可以在这里找到: https://github .com/sebastianbergmann/phpunit/pull/3042/files

public function testCodeNoAssertions()
{
    $this->expectNotToPerformAssertions();
}

至少 PHPUnit 7.0+。可以使用@doesNotPerformAssertions。当前文档没有说明何时添加此内容。 https://phpunit.readthedocs.io/en/7.0/annotations.html# doesnotperformassertions

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{

}

如果您的测试只是不完整,您应该使用 $this->markTestIncomplete() https://phpunit.readthedocs.io/en/7.0/incomplete- and-skipped-tests.html

public function testCodeThatIsIncomplete()
{
    $this->markTestIncomplete('Implementation of this test is not complete.');
}

如果您想暂时禁用测试,应使用$this->markTestSkipped()https://phpunit.readthedocs.io/ en/7.0/incomplete-and-skipped-tests.html#skipping-tests

public function testCodeThatIsDisabledTemporarily()
{
    $this->markTestSkipped('This test is disabled to test, if it is interfering with other tests.');
}

PHPUnit 7.2+. $this->expectNotToPerformAssertions() can be used, if you have conditions, that might disable assertions. Seems it is not in the official docs, but can be found here: https://github.com/sebastianbergmann/phpunit/pull/3042/files

public function testCodeNoAssertions()
{
    $this->expectNotToPerformAssertions();
}

At least PHPUnit 7.0+. @doesNotPerformAssertions can be used. Current docs do not state when this was added. https://phpunit.readthedocs.io/en/7.0/annotations.html#doesnotperformassertions

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{

}

If your test is simply incomplete, you should be using $this->markTestIncomplete() https://phpunit.readthedocs.io/en/7.0/incomplete-and-skipped-tests.html

public function testCodeThatIsIncomplete()
{
    $this->markTestIncomplete('Implementation of this test is not complete.');
}

If you want to temporarily disable a test $this->markTestSkipped() should be used. https://phpunit.readthedocs.io/en/7.0/incomplete-and-skipped-tests.html#skipping-tests

public function testCodeThatIsDisabledTemporarily()
{
    $this->markTestSkipped('This test is disabled to test, if it is interfering with other tests.');
}
雄赳赳气昂昂 2024-11-26 13:28:34

我创建了一个超类并在那里放置了类似 SimpleTest 的函数:

/**
 * Pass a test
 */
public function pass()
{
    //phpunit is missing pass()
    $this->assertTrue(true);
}

然后你可以调用它

$this->pass();

I created a superclass and placed SimpleTest -like function there:

/**
 * Pass a test
 */
public function pass()
{
    //phpunit is missing pass()
    $this->assertTrue(true);
}

Then you can call it

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