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.

  • 3
    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
    Commented May 17, 2013 at 7:27
  • hi @brasofilo I've updated my question. Commented May 17, 2013 at 13:55

1 Answer 1


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

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.