WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 5 months ago

#42278 new enhancement

Speed up tests by using shared user fixtures

Reported by: Frank Klein Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: needs-unit-tests early needs-patch
Focuses: Cc:

Description

There are a lot of tests that require user fixtures. These are then created, and afterwards deleted, as part of the test class set up and tear down methods.

These fixtures could all be reused between tests, if a user for every role in Core would be created in the database as part of the unit test setup process.

If we had that, all the tests that need for example a user with the editor role could just grab the existing user from the database, instead of creating this as a test fixture.

Attachments (1)

42278.patch (21.5 KB) - added by Frank Klein 5 months ago.

Download all attachments as: .zip

Change History (4)

#1 @johnbillion
7 months ago

  • Keywords needs-unit-tests early added
  • Milestone changed from Awaiting Review to 5.0
  • Version trunk deleted

We'll need to consider only setting up these fixtures when WP_RUN_CORE_TESTS is true, or to introduce a means of plugins/themes that use the test suite to opt in to these fixtures, otherwise this has a high chance of breaking existing plugin/theme tests that count the number of users on the site.

#2 @netweb
6 months ago

  • Keywords needs-patch added

@Frank Klein
5 months ago

#3 @Frank Klein
5 months ago

The patch above is a quick and dirty proof of concept that the approach outlined in the ticket could work, and that it would not require too many changes.

But it brings a few things to light:

  • I imagine the fixture creation to happen in a class, which is accessible in the same way in WP_UnitTestCase as the factory. So to use a shared user fixture in a test, you could to self::fixtures()->user->get_contributor() or similar.
  • Currently a lot of tests are opaque because there is already a user in the database. This could be made more transparent by using something like self::fixtures()->user->get_count() when counting users returned by queries for example.
  • There are a lot of user fixture creations in tests related to roles. So the test just needs a users with a particular role, often to check access rights. The shared fixtures could speed up tests significantly by just pulling a user object from memory.
  • We would need to fill out all user fields with dummy data, so that tests that need things like the nice name or similar could work.

I do think this is probably worth integrating into the test suite, but I'm curious to hear more feedback on this.

Note: See TracTickets for help on using tickets.