4

I've just finished writing a WordPress plugin implementing shortcode tag based autocompletion based on values retrieved from a database given an id set on a cookie.

The plugin has a variety of combinations of possible scenarios, based on parameters such as:

whether or not to display matched fields

whether to limit (matched or not) fields to be displayed to a certain maximum

whether to show a particular set of fields under any circumstances, if matched

As simple as it seems it's giving me a considerable headache (and taking a considerable amount of time) to test all scenarios after every revision. My contractor is also a little pissed off of the constant regressions and missed bugs he is finding on my code as the project gets bigger.

So, my question is quite simple: What would be the best approach for designing PHP unit tests for a plugin implementing shortcode tags in WordPress?

My best idea is to write a set of posts containing the test cases and use file_get_contents($posturl) to compare the expected content output with the actual output but there has to be a better way. Any ideas?

3
  • If you haven't studied up on WordPress's own unit testing tools and how to use them with plugins, I'd recommend that you do that. Here's a tutorial I wrote, to get you started: codesymphony.co/writing-wordpress-plugin-unit-tests
    – J.D.
    Jan 15, 2014 at 13:47
  • Wow, that was remarkably useful! Very nice article, just what I needed. Thanks a lot for putting me into the right track. Can you write your comment as an answer so I can accept it?
    – NotGaeL
    Jan 16, 2014 at 9:00
  • I've added an answer. :-)
    – J.D.
    Jan 16, 2014 at 13:40

2 Answers 2

6

If you haven't studied up on WordPress's own unit testing tools and how to use them with plugins, I'd recommend that you do that. Here's a tutorial I wrote, to get you started: http://codesymphony.co/writing-wordpress-plugin-unit-test

2
  • You mention in your tutorial that after requiring TEST_PLUGIN_FILE, the plugin needs to be installed. My question is how do you install it? Is it as simple as updating the active_plugins option? Feb 17, 2016 at 18:06
  • 1
    @henrywright That depends on the plugin. By install I mean do whatever it does when the activation hook is called (create database tables, add default options, etc.). You don't actually have to modify the active plugins option, since you are loading the plugin manually. But you could call activate_plugin( $your_plugin ) to call your plugin's activation hook, update the option, etc. If you do it that way though, WordPress will verify that your plugin is in the plugins directory, so you'll have to have a symlink of it there. I usually just call the install function for my plugin manually.
    – J.D.
    Feb 17, 2016 at 21:32
0

I wrote a tutorial on setting up a PHPUnit Test fixture for Wordpress plugin development. Have a look at http://mdwpmerchant.com.au/building-testable-phpunit-wordpress-plugin/

1
  • 1
    A link-only answer is one of the worst in all cases. Please enrich your answer with breif and then add a link for additional details. Jan 28, 2014 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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