4

I am a plugin developer with absolutely no industry experience. I have created a simple plugin to add multiple contributors to a post through metabox.

I searched through tons of websites and I got to know I need wp-cli and PHPUnit to perform Unit Tests but I have no clue what to test, what functions/hooks to include or exclude for testing, what precautions I need to take and how to feed dummy user data to the test.

Can anyone here guide me through the process?

2 Answers 2

4

Types of tests

First, note that "unit testing" in WordPress generally goes beyond unit testing, and is more like functional/integration testing. This is more of a technical difference, but if you understand the difference between these different things, you may sometimes be confused by how WordPress "unit" tests things.

When you ask what parts of the plugin you should test, ideally you should be testing all of it. But as to what parts you should unit (or integration) test, and what parts you should use acceptance tests for, is a different matter.

Currently, for my plugins, I use two different layers of tests: "unit" tests with PHPUnit, and acceptance tests run in the browser via WP Browser. The unit tests test internal logic, whereas the acceptance tests check that when a user uses your plugin's UI it actually works.

Test

The things to unit test are:

  • code that modifies values in the database
  • code that performs calculations
  • code that contains other logic

Basically, any function that you pass a value(s) to and it does something like this with it, and/or returns some value to you.

To test this kind of code, you write a test that calls the function in question, passing in particular values, and then assert that the result that is returned is what you expect. Or, for functions that modify the database, you would assert that the database contains the values that are expected after the function has been called.

Don't test

Code that is difficult to unit test, and some would perhaps argue shouldn't be unit tested, would be code that is basically just dedicated to outputting stuff into the browser. That's where acceptance tests would come in.

Also, since you mention actions/filters, it sounds like you might be wondering whether you should test that a function is hooked to the correct action, or that a function you have hooked to a filter is actually applied when WordPress calls that filter. This may or may not be needed, depending on the action or filter in question. Generally, I don't test this sort of thing, as it usually is something that is better suited to functional/acceptance tests (though acceptance tests only test them indirectly).

Summary

So basically, unit test logic and database methods, acceptance test your plugin's actual UI. In order for this to work well, of course, you need to keep your template/output code mostly separate from your other logic, which is generally a good idea anyway.

In your case, you may find that acceptance tests are really more useful for your plugin, and unit tests wouldn't really be needed, since most everything would already be tested by the acceptance tests. However, having both would generally be the ideal.

0

I'd say that, in the case of WordPress, unit tests aren't the only thing worth looking into: functional/integration/acceptance tests (you pick the types that are more relevant in your situation) are also important to have, mostly because it's an ecosystem and it's important to verify that your code works with the rest of the components (and, as a bonus, even with some of the major plugins, like WooCommerce, Yoast SEO, etc.).

For all of the above (including unit tests but not mandatory), I personally found that Codeception is a straightforward tool to get started with. Luca is doing important WordPress-related work in this area with his wp-browser extensions for Codeception, among other things. He also has an excellent blog explaining a lot of it.

Your Answer

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

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