10up / wp_mock

WordPress API Mocking Framework
https://wp-mock.gitbook.io
Other
676 stars 70 forks source link

Using wp_mocks together with WordPress core Unit tests #49

Open sudar opened 8 years ago

sudar commented 8 years ago

I know that wp_mock and WordPress core Unit tests work differently and it may not even be possible to have them run together.

I use both and like both. But there are certain tests for which I would prefer wp_mock and certain tests for which I would prefer WordPress core Unit tests. But unfortunately right now there is no way to configure both of them for a single plugin. You have to pick one and stick with it even if you can group all the 'mock' tests using a separate testsuite or phpunit group and invoke the separately from phpunit.

What I am suggesting is to have a way to setup both of them together for a plugin and invoke the appropriate framework and their tests using either phpunit's group or testsuite feature.

I know that there was some recent discussion about it and I am interested in knowing everyone's thoughts on it.

blobaugh commented 7 years ago

Seconded! Any implementation plans on this?

davetbo commented 7 years ago

If this means that I can use \WP_Mock with the unit testing scaffold that is set up by the wp-cli command line tools, then I'm interested as well!

ssnepenthe commented 7 years ago

This would be amazing but how would you even approach it?

If memory serves, once WordPress has been loaded by the core test suite, WP_Mock cannot mock core functions because they will have already been defined.

I looked into this a while back (albeit briefly) and the best I could come up with was to configure tests to run in separate processes and disable the preserve global state setting.

That effectively means you have to reload the bootstrap for every single test, however, which is far from ideal.

For now I have settled on configuring two test suites when I want to use both - each gets their own bootstrap file and I just have to be explicit about which I want to run when I call PHPUnit.

It's not pretty, but it does the trick...

ericmann commented 7 years ago

For now I have settled on configuring two test suites when I want to use both - each gets their own bootstrap file and I just have to be explicit about which I want to run when I call PHPUnit.

That's the way to make it work, unfortunately. The alternative is to run all tests using process isolation, but that causes some problems with the core test suite (it introduces a few race conditions with the database).