-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[master] Add explicit PHPUnit asserts to unit tests flagged as 'risky' by PHPUnit6 #34871
Conversation
Codecov Report
@@ Coverage Diff @@
## master #34871 +/- ##
=============================================
- Coverage 65.35% 48.39% -16.96%
=============================================
Files 1208 109 -1099
Lines 69969 10493 -59476
Branches 1280 1280
=============================================
- Hits 45727 5078 -40649
+ Misses 23870 5043 -18827
Partials 372 372
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #34871 +/- ##
============================================
+ Coverage 65.34% 65.35% +<.01%
Complexity 18484 18484
============================================
Files 1208 1208
Lines 69969 69969
Branches 1280 1280
============================================
+ Hits 45723 45728 +5
+ Misses 23874 23869 -5
Partials 372 372
Continue to review full report at Codecov.
|
@@ -93,7 +93,7 @@ public function __construct( | |||
* | |||
* @param string[] $params | |||
* | |||
* @return bool | |||
* @return void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While adjusting OcmMiddlewareTest
I saw that the PHPdoc return specified here was wrong.
This method either returns nothing (void
) or throws an exception.
$this->assertContains('Starting scan for user 1 out of 10 (user1)', $output); | ||
$this->assertContains('Starting scan for user 1 out of 10 (user11)', $output); | ||
$this->assertContains('Starting scan for user 1 out of 10 (user21)', $output); | ||
$this->assertContains('Starting scan for user 10 out of 10 (user30)', $output); | ||
} else if (\count($groups) === 4) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was doing no assertContains
checks for the data set:
[['--groups' => 'group1,group2', '--group' => ['group2','group3']], '']
and so it did not fail, but also was snake oil.
$this->assertContains('Starting scan for user 11 out of 20 (user21)', $output); | ||
$this->assertContains('Starting scan for user 10 out of 10 (user40)', $output); | ||
} else { | ||
$this->fail( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure that if someone adds test cases with different numbers of groups in the command, that the test will fail until they add appropriate asserts.
@@ -97,6 +97,10 @@ public function testEmittingCallWithAdditionalArgument($className, $eventName, $ | |||
$this->assertInstanceOf(GenericEvent::class, $calledAfterEvent[1]); | |||
$this->assertArrayHasKey('item', $calledAfterEvent[1]); | |||
$this->assertEquals('testing', $calledAfterEvent[1]->getArgument('item')); | |||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously no checks were happening for the test cases that had an empty after
specified. So those test cases were "snake oil". Actually it turns out that the tests are OK - checking with these asserts passes.
$this->backend->createGroup($group), | ||
"there was a problem creating $group" | ||
); | ||
$this->assertFalse( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first time calling createGroup()
returns true (it did it)
The second time returns false - the group already exists.
We can check this stuff, then we will know if that behavior changes.
57b8e0d
to
568cb77
Compare
Backport |
@phil-davis can we configure this in PHPUnit so it runs like we want to instead of skipping tests ? |
@PVince81 I don't understand what you mean? All the tests are actually run by For safety, I put an Of course all tests can fail if something that they call throws an exception, but |
05d2a6e
to
d0f643c
Compare
d0f643c
to
d83e81a
Compare
@phil-davis I was a bit out of context. I'm surprised that PHPUnit 5 doesn't report such tests as risky already ? Or did we have an option in phpunit.xml to disable this ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 fine by me
PHPUnit6 seems to report the risky stuff. The default for |
Description
PHPUnit6 reports as "risky" tests that ran the test case but no
assert
was ever done.Those are suspicious, because they might never fail.
Often the tests are checking that "something works and does not throw an exception". In that case the test will fail if an exception is thrown (for whatever reason - bug...) In those cases, put an assert of some sort about what the method call in the test is expected to return. If it is a method call that returns something, e.g. returns an int, then check that the return value is an int. If it is a method that does not return anything ("return void") then use
assertNull
to check that the return value "is not".And fix other tests that turned out to be "snake oil" on examination (see inline comments).
It seems that the reporting of these sort of tests as "risky" is worth having.
Related Issue
Part of the rabbit-hole being followed from issue #34858
Motivation and Context
How Has This Been Tested?
CI
Types of changes
Checklist:
Open tasks: