Why is WP_Mock not used instead of WP_UnitTestCase for writing unit tests by most plugins?

For writing unit-tests, why do most plugins use WP_UnitTestCase instead of popular libraries like WP_Mock?
The reason I am asking this is because WP_UnitTestCase doesn’t seem to be a unit test case but integration test as it requires WordPress-test environment & make actual DB-calls to WordPress-test-db? While WP_Mock mocks all WP functions and you can test class in isolation(which is also the definition of unit tests). Is there anything I am missing out here?

1 Answer
1

One is official, the other one is a 3rd party library by 10up, so people tend to use the official WP_UnitTestCase

There is no right or wrong choice however, use the library you prefer

Is there anything I am missing out here?

No, some tests that core conducts require a temporary WP install to fully test. Some older APIs weren’t built for test isolation. As such, WP_UnitTestCase and WP_Mock are both capable of unit tests, but WP_UnitTestCase is also used for other kinds of tests.

For your purposes as a plugin or theme author though, that particular information is irrelevant, and both are options. It really is up to your personal preference

Leave a Comment