-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assert no Exception happened #2484
Comments
PHPUnit is now strict about useless tests by default. If you do not want this then simply run your tests using the |
@sebastianbergmann that is the problem... I want the risky test report to be turned on, but I also want a meaningful way of correcting my test cases instead of useless true===true assertions. |
Once I also felt the need to checking if no exception was thrown. But then I made a small deduction:
|
PHPUnit 6.1 will always display details about risky tests. PHPUnit 6.0 requires |
The |
The problem now is that tests with no assertion are not considered covered in the coverage report. |
My particular use case is a regression test. The condition used to throw an exception, but now that I've fixed the problem I want to assert that the code runs without an exception being raised. I'd prefer to do this explicitly rather than making a dummy assertion as above and commenting in code. |
@sebastianbergmann This sounds like a hack. |
When I use the annotation, I do have the coverage. First, I have investigated the code and found no reason why it would affect coverage, then I executed sample test class using annotation for every single test, coverage result is the same as without annotation:
without annotation: |
I know this is a closed issue, but just for others who get here (like me) after a quick research from Google. public function testMyMethod_WillNotThrowMySpecificException()
{
$anExceptionWasThrown = false;
try {
$sut->myMethod();
} catch (MySpecificException $e) {
$anExceptionWasThrown = true;
}
$this->assertFalse($anExceptionWasThrown);
} This way the test is specific to YOUR code, and others exceptions thrown will be catch as attempted by PHPUnit. |
Another one stumbling on this. I fixed this a bit differently to get a bit more sensible errors messages, since using true makes it harder to track what is going on: public function testAssertionPassesWhenFooBarIsAvailable()
{
try {
$this->fooBarAvailabilityChecker->assertFooBarAvailable();
$this->addToAssertionCount(1);
} catch (FooBarNotAvailable $exception) {
$this->fail($exception->getMessage());
}
} |
Until PHPUnit 6, a successful execution could be left without any assertion, while faulty situations were captured using Exceptions. Now on PHPUnit 6, successful executions are being considered risky (R), and sometimes no assertion at all is needed.
We should be able to flag that no exceptions happened during the execution of the method, which would be a much better replacement than the current alternative
self::assertTrue(true);
.The text was updated successfully, but these errors were encountered: