Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#60426 closed enhancement (fixed)

Update WP_Test_REST_TestCase::assertErrorResponse() to allow custom failure messages

Reported by: antonvlasenko's profile antonvlasenko Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.6 Priority: normal
Severity: trivial Version:
Component: Build/Test Tools Keywords: good-first-bug has-patch has-unit-tests
Focuses: coding-standards Cc:

Description

According to the WordPress coding standards, it is recommended that if there are more than 2 assertions in a single test method, these assertions should include custom failure messages to explain the nature of any failed assertions.
However, in the current state of the WP_Test_REST_TestCase::assertErrorResponse() helper method, it contains 2 assertions, making it impossible to add a custom message explaining the error.

To align with coding standards and improve the clarity of test failures, I propose updating the method's signature as follows:

protected function assertErrorResponse($code, $response, $status = null, $failure_message)

This change will allow developers to include a custom message when using the WP_Test_REST_TestCase::assertErrorResponse() method, improving the ability to diagnose issues during testing.

Furthermore, all the instances where this method is being used across the codebase need to be refactored to include the new $failure_message parameter, ensuring consistency.

Change History (8)

#1 @antonvlasenko
2 years ago

  • Severity changed from normal to trivial

#2 @swissspidy
2 years ago

  • Component changed from REST API to Build/Test Tools
  • Keywords good-first-bug added

Furthermore, all the instances where this method is being used across the codebase need to be refactored to include the new $failure_message parameter, ensuring consistency.

This is best done on an ad-hoc basis when touching those tests anyway.

#3 @SergeyBiryukov
2 years ago

  • Milestone changed from Awaiting Review to 6.6

Thanks for the ticket!

Just noting that the parameter name should probably be $message rather than $failure_message, for consistency with PHPUnit's own assertion methods, as well as custom assertions in WP_UnitTestCase_Base, see [51478] / #53363.

This ticket was mentioned in PR #6404 on WordPress/wordpress-develop by mykola.


2 years ago
#4

  • Keywords has-patch has-unit-tests added; needs-patch removed

#5 @SergeyBiryukov
2 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#6 @SergeyBiryukov
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 58039:

Tests: Add a $message parameter for a custom assertion in WP_Test_REST_TestCase.

All assertions in PHPUnit have a $message parameter. Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertions, making debugging of the tests easier.

This optional parameter is now added for WP_Test_REST_TestCase::assertErrorResponse().

Follow-up to [34928], [51478].

Props mykolashlyakhtun, antonvlasenko, swissspidy, SergeyBiryukov.
Fixes #60426.

@SergeyBiryukov commented on PR #6404:


2 years ago
#7

Thanks for the PR! Merged in r58039.

#8 @SergeyBiryukov
2 years ago

In 58051:

Tests: Add more specific messages for a custom assertion in WP_Test_REST_TestCase.

All assertions in PHPUnit have a $message parameter. Setting this parameter allows to distinguish which assertion is failing when a test runs multiple assertions, making debugging of the tests easier.

Follow-up to [34928], [51478], [58039].

See #60426.

Note: See TracTickets for help on using tickets.