Skip to content
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

Merged
merged 3 commits into from
Mar 28, 2019

Conversation

phil-davis
Copy link
Contributor

@phil-davis phil-davis commented Mar 22, 2019

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Database schema changes (next release will require increase of minor version instead of patch)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation ticket raised:

Open tasks:

  • Backport (if applicable set "backport-request" label and remove when the backport was done)

@phil-davis phil-davis self-assigned this Mar 22, 2019
@phil-davis phil-davis changed the title Add explicit PHPUnit asserts to unit tests flagged as 'risky' Add explicit PHPUnit asserts to unit tests flagged as 'risky' by PHPUnit6 Mar 22, 2019
@codecov
Copy link

codecov bot commented Mar 22, 2019

Codecov Report

Merging #34871 into master will decrease coverage by 16.95%.
The diff coverage is n/a.

Impacted file tree graph

@@              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
Flag Coverage Δ Complexity Δ
#javascript 53.04% <ø> (ø) 0 <ø> (ø) ⬇️
#phpunit 38.17% <ø> (-28.59%) 0 <ø> (-18484)
Impacted Files Coverage Δ Complexity Δ
lib/private/Files/Storage/DAV.php 59.45% <0%> (-21.64%) 0% <0%> (ø)
apps/updatenotification/templates/admin.php
lib/private/Encryption/Keys/Storage.php
lib/private/App/CodeChecker/NodeVisitor.php
lib/private/RedisFactory.php
apps/dav/lib/Avatars/AvatarNode.php
...s/dav/appinfo/Migrations/Version20170202213905.php
apps/dav/lib/Upload/ChunkLocationProvider.php
apps/files/lib/AppInfo/Application.php
apps/systemtags/list.php
... and 1090 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa3a310...57b8e0d. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 22, 2019

Codecov Report

Merging #34871 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@             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
Flag Coverage Δ Complexity Δ
#javascript 53.04% <ø> (ø) 0 <ø> (ø) ⬇️
#phpunit 66.76% <ø> (ø) 18484 <ø> (ø) ⬇️
Impacted Files Coverage Δ Complexity Δ
...eratedfilesharing/lib/Middleware/OcmMiddleware.php 77.27% <ø> (ø) 20 <0> (ø) ⬇️
lib/private/Files/View.php 84.6% <0%> (+0.3%) 398% <0%> (ø) ⬇️
lib/private/DB/Connection.php 66.17% <0%> (+0.73%) 49% <0%> (ø) ⬇️
apps/files_trashbin/lib/Expiration.php 98.27% <0%> (+1.72%) 29% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 51e12cf...d83e81a. Read the comment docs.

@@ -93,7 +93,7 @@ public function __construct(
*
* @param string[] $params
*
* @return bool
* @return void
Copy link
Contributor Author

@phil-davis phil-davis Mar 22, 2019

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) {
Copy link
Contributor Author

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(
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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(
Copy link
Contributor Author

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.

@phil-davis phil-davis force-pushed the add-assert-to-risky-unit-tests-phpunit-5 branch from 57b8e0d to 568cb77 Compare March 22, 2019 08:44
@phil-davis phil-davis changed the title Add explicit PHPUnit asserts to unit tests flagged as 'risky' by PHPUnit6 [master] Add explicit PHPUnit asserts to unit tests flagged as 'risky' by PHPUnit6 Mar 22, 2019
@phil-davis
Copy link
Contributor Author

Backport stable10 #34876

@PVince81
Copy link
Contributor

@phil-davis can we configure this in PHPUnit so it runs like we want to instead of skipping tests ?

@phil-davis
Copy link
Contributor Author

phil-davis commented Mar 22, 2019

@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 phpunit, it is just that some of them contain no assertSomething() calls, and so phpunit (after running them) reports that they are "risky" because they "cannot fail" in a typical way.

For safety, I put an assertSomething() call in each offending test. That makes sure that the test always asserts something, and thus could fail if the assertion is not true at run-time.

Of course all tests can fail if something that they call throws an exception, but phpunit does not consider only having that to be "good practice". This thread has lots of discussion related to "Testing an exception is not thrown":

sebastianbergmann/phpunit-documentation#171 (comment)

@phil-davis phil-davis force-pushed the add-assert-to-risky-unit-tests-phpunit-5 branch from 05d2a6e to d0f643c Compare March 24, 2019 04:44
@phil-davis phil-davis force-pushed the add-assert-to-risky-unit-tests-phpunit-5 branch from d0f643c to d83e81a Compare March 25, 2019 05:12
@PVince81
Copy link
Contributor

@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 ?

Copy link
Contributor

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 fine by me

@phil-davis
Copy link
Contributor Author

@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 ?

PHPUnit6 seems to report the risky stuff. The default for beStrictAboutTestsThatDoNotTestAnything was false in PHPUnit5, now the default is true. So we suddenly start seeing these reported in PHPUnit6.

@phil-davis phil-davis merged commit 3f4a433 into master Mar 28, 2019
@delete-merged-branch delete-merged-branch bot deleted the add-assert-to-risky-unit-tests-phpunit-5 branch March 28, 2019 15:16
@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants