Make WordPress Core

Opened 4 years ago

Last modified 3 months ago

#38266 new defect (bug)

Tests: Use assertSame() when the type of the value in the assertion is important

Reported by: johnbillion Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: needs-unit-tests
Focuses: Cc:


Several tests use assertEquals() on a falsey value where the type is important to the accuracy of the test.

For example, in Tests_Cache::test_miss(), a value is tested against an expected NULL value using assertEquals() which will produce a false positive if another falsey value such as 0, false, or an empty string is passed. These means these such assertions are actually buggy.

Using assertSame() instead of assertEquals() will cause the type to be checked in addition to its value.

Change History (4)

#1 @johnbillion
4 years ago

Looks like two of the assertions in Tests_Cache are incorrect.

  1. test_miss() - false is returned from a cache miss, not null.
  2. test_add_get_null() - a null value is retained, not converted to an empty string.

These will need more investigation.

#2 @johnbillion
15 months ago

  • Keywords needs-unit-tests added; ongoing removed

#3 @johnbillion
13 months ago

I wrote a package to detect usage of assertEquals() on falsey values: https://github.com/johnbillion/falsey-assertequals-detector. If someone wants to pick this up, that package could be used to start addressing these cases in core.

This ticket was mentioned in Slack in #core-multisite by johnbillion. View the logs.

3 months ago

Note: See TracTickets for help on using tickets.