WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently doing some modifications on the Wordpress plugin that I created in order to make it testable. The problem is I don't know which methods am I suppose to write tests for. I have methods which uses some of these functions and global variables:


is_singular, is_page, is_home and other functions depends on the context. I'm not sure how to fake those context in a test.

I tried passing those as arguments to the methods instead but there are some hooks that doesn't accept additional arguments so I can't really pass them.

Is it OK to just supply those from the test class? I'm really having difficulty modifying the plugin in such a way that I don't use those.

Any ideas?


I'm using wordpress-tests and PHP Unit for the unit testing. I've done some research on unit testing before I started and there's a practice that when unit testing global variables should be avoided.

share|improve this question
What do you mean by "testable"? What tests are you conducting and how are you watching/inspecting them? Why do you need to pass functions and globals as arguments, when you can access them directly inside the method? Or call the method conditionally using them? By "test", do you mean debug? – brasofilo May 17 '13 at 7:27
hi @brasofilo I've updated my question. – Ieyasu Sawada May 17 '13 at 13:55
up vote 4 down vote accepted

WordPress is far from having a standard conventions for unit testing extensions. Or for extension structure in general.

At current time you should either:

  • research how unit testing framework of your choice recommends to work with globals and such for PHP code in general
  • look into WordPress-specific tools for unit testing that start to appear, such as WP_Mock
share|improve this answer

Your Answer


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.