PHPUnit 通过配置中没有断言的测试
我想通过获得以下内容的测试:“此测试没有执行任何断言”
我知道我可以添加类似 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
编辑:您有几种选择,具体取决于您使用的版本、是否要忽略所有有风险的测试或只是少数,以及是否需要是永久的还是临时的。
在 5.6 之前,如果您不想在所有测试中添加虚假断言,则必须避免将
--strict
传递给 PHPUnit 或添加strict="false"
到您的phpunit.xml
。此选项的要点是“如果未做出断言,则将测试标记为不完整”。在某些时候,PHPUnit 添加了相关的
--dont-report-useless-tests
命令行开关和beStrictAboutTestsThatDoNotTestAnything="false"
配置选项。我还没有检查它们是否是替代品或额外的细粒度版本。上述选项影响所有有风险的测试。使用它们会让您意外地编写没有断言的测试。以下新选项更安全,因为您必须有目的地标记您希望允许的每个有风险的测试。
PHPUnit 5.6 添加了
@doesNotPerformAssertions
注释将单个测试用例标记为“没有风险”,即使他们没有做出任何断言。PHPUnit 7.2 引入了
TestCase::expectNotToPerformAssertions()
其中做同样的事情。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 addstrict="false"
to yourphpunit.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 andbeStrictAboutTestsThatDoNotTestAnything="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.PHPUnit 7.2 introduced
TestCase::expectNotToPerformAssertions()
which does the same thing.使用 PHPUnit 7.2,您可以获得另一种选择:
另请参阅 https://github.com/sebastianbergmann/phpunit/pull /3042。
With PHPUnit 7.2 you get another alternative:
See also https://github.com/sebastianbergmann/phpunit/pull/3042.
使用
@doesNotPerformAssertions
注解:Use the
@doesNotPerformAssertions
annotation:使用
$this->addToAssertionCount(1)
。见下文。Make use of
$this->addToAssertionCount(1)
. See below.就我而言,测试没有断言,因为我只是使用
prophecy
lib 检查所有内容来模拟并检查方法是否按预期调用...PHPUnit 默认情况下期望测试至少有一个断言,并警告你这样说。
只需在测试的任何部分添加这一行就足够了。
但你也可以设计一个测试,在某些情况下有断言或没有断言
总之,这不是一个错误......这是一个功能。
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.
But also you can design a test that in some cases have assertions or not
In conclusion, It's not a bug... It's a feature.
如果您有 PHP SimpleTest,另一种方法是使用:
这会将测试标记为已完成并通过。
另一方面,对于您想要失败的测试,您可以使用:
例如:
我在 PHP 5.5 中尝试了这个并且有效:
输出:
也许方法传递在 PHPUnit 中仍然可以轻松实现。来源来自SimpleTest:
If you have PHP SimpleTest, another approach would be to use:
This will mark the test as completed and passed.
On the other hand, for a test that you want to fail, you can use:
For example:
I tried this in PHP 5.5 and works:
Output:
Maybe the method pass can still be easily implemented in PHPUnit. Source from SimpleTest:
PHPUnit 7.2+。如果您有条件,可以使用 $this->expectNotToPerformAssertions() ,这可能会禁用断言。似乎不在官方文档中,但可以在这里找到: https://github .com/sebastianbergmann/phpunit/pull/3042/files
至少 PHPUnit 7.0+。可以使用@doesNotPerformAssertions。当前文档没有说明何时添加此内容。 https://phpunit.readthedocs.io/en/7.0/annotations.html# doesnotperformassertions
如果您的测试只是不完整,您应该使用 $this->markTestIncomplete() https://phpunit.readthedocs.io/en/7.0/incomplete- and-skipped-tests.html
如果您想暂时禁用测试,应使用$this->markTestSkipped()。 https://phpunit.readthedocs.io/ en/7.0/incomplete-and-skipped-tests.html#skipping-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
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
If your test is simply incomplete, you should be using $this->markTestIncomplete() https://phpunit.readthedocs.io/en/7.0/incomplete-and-skipped-tests.html
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
我创建了一个超类并在那里放置了类似 SimpleTest 的函数:
然后你可以调用它
I created a superclass and placed SimpleTest -like function there:
Then you can call it