2

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

1 Answer 1

5

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

4
  • Tom, apart from 3rd party part, they still don't seem same to me. WP_UnitTestCase is doing integration tests instead of unit-tests because you need Wordpress test environment and functions make actual database calls in test_db. While WP_Mock has already mock all those functions and you can test the class in isolation. Just wondering, why many plugins don't use libraries like WP_Mock which actually allows you to do unit tests without any dependency on outside world. P.S: Forgive my ignorance(if any) as I a newbie in unit-testing in wordpress Jan 12, 2018 at 14:03
  • 1
    That's because they aren't, they're built by different people. I'm not sure what it is you're asking now. I thought you asked why people tend to use WP_UnitTestCase, and I answered that it's because it's an official library. If you don't have a clear question can you either edit your question so that it can be concretely and canonically answered, else if you're actually looking for a discussion then perhaps this question should be closed and asked instead on the discussion forums? WP_UnitTestCase is a class core uses to assist in writing tests. Not all those tests are unit tests
    – Tom J Nowell
    Jan 12, 2018 at 14:21
  • Thanks, Tom. I have edited my question to be more specific about my query Jan 12, 2018 at 14:38
  • 1
    It's still somewhat open ended, you can use both to construct unit tests, not all WP_UnitTestCase usage is for unit tests, it's still not clear what it is that you're unclear about. Are you looking for reassurance? You can use both or either to create unit tests, neither is an incorrect choice, both can be used for testing, I suspect you're looking for something more that simply isn't there, such as a particular use case you're unaware of that doesn't exist
    – Tom J Nowell
    Jan 12, 2018 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.