How do you construct unit tests with PHPUnit and the WordPress test framework that simulate being in the loop? For instance, if I want to test a function that is only able to be called in the loop because it assumes the post data is set up appropriately for the main query, how would I construct that unit test?

  • 3
    Technically, that is an integration test, not a unit test. You are testing more than one unit of the code … and the whole WordPress.
    – fuxia
    Mar 1, 2016 at 19:32
  • 1
    For "real" unit tests brain-wp.github.io/BrainMonkey :)
    – gmazzap
    Mar 2, 2016 at 0:33

3 Answers 3


The key for that is the go_to method of WP_UnitTestCase class that you should be extending. It simulates parsing a url relative to the wordpress root (IIRC). You might need to first set posts and other info, and then call go_to to trigger the loop.

  • What does this sentence mean? It simulates parsing a url relative to the wordpress root (IIRC). Jun 19, 2016 at 1:25
  • @JimMaguire That means that the class passes a URL that is not an absolute one, but one relative to the directory that WordPress is installed in. See this post for a primer on this.
    – Tim Malone
    Jun 29, 2016 at 3:29

Assuming that you're using WP_UnitTestCase, try this inside your test class:

private $post_id;

// use the factory to create a post with some fake data
public function setUp() {

    $this->post_id = $this->factory()->post->create( 
        array ( 'post_content' => 'Here are some words. <img src="image.jpg"> <!--more--> And here are some more' ) 


public function test_a_thing() { 

    // fake going to the URL
    $this->go_to( get_permalink( $this->post_id ) );

    // make sure your relevant globals are set
    global $post;

    // profit

Unit testing plugin functions in Wordpress is extremely difficult. It's usually easier to do an acceptance or functional test. The difference is that in a unit test, you have to mock everything you interact with. In a functional test, you can run the whole system. For instance, I use Codeception to launch a browser and test against the whole system. Opinions vary, but I think the whole concept of unit testing a WP plugin is screwy. The plugin doesn't exist in a vacuum. It's a system that interacts with another system. What's the point of even TRYING to isolate? BTW - Almost no devs actually test this way. The state of WP testing frameworks is SORRY.

  • 1
    The point of unit testing is for detecting regression failures. It is best when testing less traveled paths of the code, especially ones which might be hard to recreate like exception handling or external messaging. Because they are focused on small units of code there is less effort required in maintaining the test code itself with every feature change. Jun 19, 2016 at 3:09
  • 1
    In acceptance tests every change of a menu item requires you to rewrite most of your tests. It is not that acceptance testing is not needed when you have unit tests, but that you can focus testing the main paths and "trust" unit testing to cover the "edge" cases. Jun 19, 2016 at 3:12
  • Yes, you're correct. It's just been my experience that unit testing basically isn't being done at all in 99% of WP plugin development. [As opposed to WP core, which is a single system and is unit tested]. I guess my answer is wrong, but I'm sticking to it. The best way to unit test WP plugin code is to not unit test it, and do functional or acceptance testing. Jun 19, 2016 at 3:34
  • It's true if you use a recorder that modifying a menu or something can screw it up. I'd generally recommend not doing that, and hard code your Selenium behavior. It makes the tests inherently less brittle. Jun 19, 2016 at 3:38
  • 1
    The lack of UT (and even in core the UT probably do not cover more then 50% of the functionality also due to hard to test bad spaghetti code), is just a result of tendency to hack code instead of properly develop one. 90% of plugins probably do not have any QA at all except for doing some sample input tests. At this stage IMO the discussion is not about which kind of automated testing to do, but why to do any at all :( Jun 19, 2016 at 4:15

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.