Opened 11 years ago
Closed 11 years ago
#27410 closed defect (bug) (worksforme)
Clear object cache between tests
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | close |
Focuses: | Cc: |
Description
Working on some testing stuff at the moment, and it turns out that without clearing the user cache between tests, using $this->factory->user->create
in setUp
won't work.
Steps to reproduce:
- Create test case class
- Add
$this->factory->user->create()
tosetUp
- Add more than one test method
The current workaround is to call clean_user_cache( $this->test_user );
in tearDown
, but it seems that clearing the whole of the object cache should be handled in tearDown
automatically?
Change History (3)
#2
follow-up:
↓ 3
@
11 years ago
- Keywords close reporter-feedback added
Should we just not load object caching when testing?
We can't turn it off across the board, because many of our tests are for caching and cache invalidation.
but it seems that clearing the whole of the object cache should be handled in tearDown automatically?
Strictly speaking, we should run it during setUp()
, to be absolutely sure that the cache is clean *before* each test is run. And, lo and behold, we do! https://core.trac.wordpress.org/browser/tags/4.0/tests/phpunit/includes/testcase.php#L39
My guess is that you are not calling parent::setUp()
in your test class's setUp()
method. It should be the first thing anytime you override WP_UnitTestCase::setUp()
. See eg https://core.trac.wordpress.org/browser/tags/4.0/tests/phpunit/tests/post.php.
#3
in reply to:
↑ 2
@
11 years ago
- Keywords reporter-feedback removed
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
Replying to boonebgorges:
Strictly speaking, we should run it during
setUp()
, to be absolutely sure that the cache is clean *before* each test is run. And, lo and behold, we do! https://core.trac.wordpress.org/browser/tags/4.0/tests/phpunit/includes/testcase.php#L39
My guess is that you are not calling
parent::setUp()
in your test class'ssetUp()
method. It should be the first thing anytime you overrideWP_UnitTestCase::setUp()
. See eg https://core.trac.wordpress.org/browser/tags/4.0/tests/phpunit/tests/post.php.
Aha. I am calling that, but the canonical memcache extension doesn't obey cache flushes on multisite. Not entirely sure why.
(Also, weird that flush_cache
reaches into the internals of the cache implementation. Really not a fan of that.)
Closing for now, as I think this is a bug in the memcache object cache.
Looks like the real issue here is that the object cache is loaded at all. Unfortunately, looks like there's no nice way to test if we're running the test suite, but detecting whether
WP_TESTS_FORCE_KNOWN_BUGS
is defined is probably Good Enough there.Should we just not load object caching when testing?