#56800 closed defect (bug) (fixed)
Tests: Reduce usage of assertEquals for 6.2
Reported by: | desrosj | Owned by: | hellofromTonya |
---|---|---|---|
Milestone: | 6.2 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | has-patch has-unit-tests commit |
Focuses: | Cc: |
Description
Follow-up to:
The assertEquals()
test method does not check that the types of the expected and actual values match. This can hide subtle bugs especially when the values are falsey.
Tasks:
- Switch to using
assertSame()
when the type of the value in the assertion is important - Replace overall usage of
assertEquals()
with type-strict assertion methods, with the aim of potentially removing its usage altogether
To help ease the effort of backporting tests, changes should also be made upstream in the Gutenberg repository.
Change History (17)
This ticket was mentioned in PR #4132 on WordPress/wordpress-develop by @Rahmohn.
20 months ago
#1
- Keywords has-patch has-unit-tests added
This ticket was mentioned in Slack in #core by costdev. View the logs.
20 months ago
This ticket was mentioned in PR #4166 on WordPress/wordpress-develop by @costdev.
20 months ago
#4
This ensures that not only the return values match the expected results, but also that their type is the same.
Trac ticket: https://core.trac.wordpress.org/ticket/56800
#5
@
20 months ago
I've submitted PR 4166 to replace some occurrences of assertEquals()
with assertSame()
. These are switcharoo changes that don't involve type-coercion or any other change.
It seems that only 7 new instances of assertEquals()
have been introduced since last cycle (improvement!), with an additional one that was missed during the last attempts to reduce assertEquals()
usage.
Pinging @hellofromTonya for review of PR 4166.
Note that there are 462 uses of $this->assertEquals()
remaining in the test suite. Many of these are testing objects, and are therefore fine. However, I think we should pursue the intention to add two new assertion conventions in 6.3:
assertSimilarObject()
- A new assertion that acts as a wrapper forassertIsObject()
andassertEquals()
when comparing objects.assertArrayEquals()
- An existing assertion for use when comparing arrays containing at least one object.
Why?
Every cycle, we have to sift through these types of assertions to determine whether their usage is fine. This is a huge time sink, and as shown by the data above, meant that putting PR 4166 together required reviewing 470 instances of assertEquals()
, many of which were testing objects/arrays with objects. The above assertions would remove some of this significant overhead, as well as add some context to the assertions.
When?
I can submit a PR for the new assertSimilarObject()
assertion as soon as we branch for 6.3. There was support for using these assertions in previous cycles, but we never got around to it.
Other remaining instances include:
$expected
is not the same type as$actual
(usually forint
orfloat
when$actual
isstring
).- There is no need for these to be
assertEquals()
when a simple type change will make the test accurate.
- There is no need for these to be
$actual
is returning afloat
when it's documented to returnint
.- This is essentially an issue with some
ceil()
usage in Core that needs appropriately rectified. Note:(int)
casts alone are not always appropriate.
- This is essentially an issue with some
- The assertion should be changed to one that isn't
assertSame()
. These include:assertTrue()
,assertFalse()
,assertNull()
,assertSameSetsWithIndex()
,assertEqualSetsWithIndex()
* andassertEqualSets()
*.
*
Requires further review if the two new assertions are implemented.
#6
@
19 months ago
- Owner set to hellofromTonya
- Status changed from new to reviewing
Thank you @costdev. Reviewing now.
This ticket was mentioned in PR #4178 on WordPress/wordpress-develop by @hellofromTonya.
19 months ago
#10
Follow-up to PR #4166.
Changes from assertEquals()
to assertSame()
. Why? To ensure both the return value and data type match the expected results.
Changes the expected height and width from string
to integer
data types. Why integer? getimagesize()
(within wp_getimagesize()
) will return an integer for both height and weight.
PR is a confidence check before committing.
Trac ticket: https://core.trac.wordpress.org/ticket/56800
@hellofromTonya commented on PR #4178:
19 months ago
#12
Committed via https://core.trac.wordpress.org/changeset/55467.
@hellofromTonya commented on PR #4166:
19 months ago
#14
Closing as changes were committed.
This PR updates the test
test_wp_count_attachments_should_cache_the_result
to check the properties of the object returned instead of checking the object withassertEquals
. That way, we guarantee the expected properties of the object.Trac ticket: https://core.trac.wordpress.org/ticket/56800