Opened 7 years ago
Last modified 6 years ago
#42278 new enhancement
Speed up tests by using shared user fixtures
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | needs-unit-tests has-patch early dev-feedback |
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)
Change History (7)
#1
@
7 years ago
- Keywords needs-unit-tests early added
- Milestone changed from Awaiting Review to 5.0
- Version trunk deleted
#3
@
7 years 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 toself::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.
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.