Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to apply unit testing for one of my plugins. I recently re designed it into classes, so unit testing should be easier to apply now.

What are some effective strategies to unit test my plugin? I need a way to use WordPress' functions used in the plugin, but I shouldn't need a running WordPress site (just test data on the DB AFAIK), right?

I have a hard time figuring out how to just unit test the plugin and not the plugin in the site's context. I could use Selenium for this, but right now I just want to focus on unit testing the plugin classes functionality, so that I don't break them with each update.

share|improve this question

3 Answers 3

up vote 12 down vote accepted

As an ex-software engineer building large business types who landed in an interactive agency let me give you a few thoughts on testing when developing for Wordpress:

  1. Your Unit Testing should test the smallest amount of behavior that a class can perform. Each class should be able to be tested independently of Wordpress. If you have a Wordpress dependency consider creating mock objects and/or methods in your test to work around this. Ultimately, this will change how you write your code a bit -- but it will be far more testable. As usual no free lunch.

  2. When you get up to the level of functional testing this is where you can test you code with Wordpress dependencies. This is where you would actually test that your plugin actually performs a service it is supposed to.

  3. Depending on what your plugin does -- consider using Selenium-based tests which test for the presence of data in the DOM by using IDs. Its reasonably robust and it is a lot less likely to break version to version. Again, the trick here is to think of testing as part of your software development lifecycle. Large changes in the code will cascade down to large changes in your tests. All part of the cost of doing business.

If you need any assistance with any of the tools to accomplish this -- I would be happy to answer if you open up a new question. I make use of both PHPUnit and its selenium extension in the work I do.

share|improve this answer
1  
Hey Ethan, thanks for such a professional answer. Couldn't expect less from this community :) I'll try to write Unit Tests for my classes and create some mock objects to work around WordPress' functions. Step 2 is the actual hard part for me, since I will need having a WordPress instace for some kind of automatic testing, where I could start using Selenium later. I'll start with PHPUnit and see how that goes. Thanks! –  Fernando Briano Feb 16 '11 at 4:50
    
Perfect answer! –  rsman Aug 22 '12 at 5:55

I've not actually done it myself, but there is a testing environment for WordPress unit testing. It's meant for core testing, but it could be adapted to test plugins.

More info here:

http://codex.wordpress.org/Automated_Testing

http://svn.automattic.com/wordpress-tests/

share|improve this answer

I have recently created a tutorial that takes the info from my googling, to show how to:

  1. Use the wordpress core make unit tests
  2. Hide php deprecated/session sent warnings
  3. Show how to set your plugin options in the tests
  4. Use same wp core make unit tests to test multiple plugins

The tutorial is here, I hope it helps

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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