PHPUnit passing a test with no assertions within config
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开发者_C百科 happens since version PHPUnit 3.5.0 with the introduction of
--strict
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;
}
Use the @doesNotPerformAssertions
annotation:
/**
* @doesNotPerformAssertions
*/
public function testCodeWithoutUsingAssertions()
{
// do stuff...
}
With PHPUnit 7.2 you get another alternative:
public function testCodeWithoutUsingAssertions()
{
$this->expectNotToPerformAssertions();
// do stuff...
}
See also https://github.com/sebastianbergmann/phpunit/pull/3042.
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);
}
}
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;
}
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.
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.');
}
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();
精彩评论