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

In recent versions WP_UnitTestCase has included a $factory property.

For example:

$post = $this->factory->post->create();

Where can I find documentation on this useful feature?

share|improve this question
up vote 15 down vote accepted

As far as I know, there is no documentation for it at present. The official source is here.

I have also written a tutorial on unit testing WordPress plugins, which gives some detail about this feature.

One of the advantages of using WP_UnitTestCase is its factories. These can be accessed through the factory member variable. The factory is an object with properties that are each an instance of one of the classes defined in includes/factory.php. What do they do, you ask? They make it very simple to create users, posts, terms, etc., wherever you need them in your test. So, instead of doing this:

$args = array( /* A bunch of user data you had to make up */ ); wp_insert_user( $args );

You can just do this:

$user_id = $this->factory->user->create();

But wait, it gets even better. What if you need many users (or posts, or whatever)? You can just create them in bulk like this:

$user_ids = $this->factory->user->create_many( 25 );

That will create 25 users that you can use in your test.

The factory has the following properties that you can use:

  • $post
  • $attachment
  • $comment
  • $user
  • $term
  • $category
  • $tag
  • $blog

They may all be used in the same manner as demonstrated in the above example with the $user factory. For example, you can create a post like this:


You can also specify particular arguments to use for creating the object. In the above example we created a post, but it wasn't assigned to a particular user (the post_author field will default to 0). Sometimes we may want the post assigned to a user instead. We'd do that like this:

$user_id = $this->factory->user->create();

$post_id = $this->factory->post->create( array( 'post_author' => $user_id ) );

Also, if you need more than just the ID of the object you are creating, you don't need to do this:

$post_id = $this->factory->post->create();

$post = get_post( $post_id );

Instead, use the create_and_get() method:

// $post will be an instance of WP_Post

$post = $this->factory->post->create_and_get();

In this example, we used the post factory, but the same is true for all of the factories.

I think I'll mention this to the WordPress docs team. Maybe we can get this stuff into the plugin and theme handbooks.

Update (June 20, 2015): You can also create your own custom factories!

share|improve this answer

The source code at


seems to be the best place to look at the moment

share|improve this answer
Just remove the link from your question: What would you read than? Please always write answers that don´t rely on outside sources. Else we´ll need to delete your answer. – kaiser Mar 25 '14 at 12:55
@kaiser, I see where you're coming from, but the answer to my question will be a link. Surely you wouldn't expect someone to post the entire documentation here? – djb Mar 25 '14 at 14:27
Yes, we would expect someone to post all relevant code here. This is even written into the site FAQ-- "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." That may not be the entire content of the linked resource but in this case it may be. As is, when that GitHub resource becomes inaccessible, this answer becomes meaningless. – s_ha_dum Mar 25 '14 at 16:26

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.