You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To figure out how many assertions you need in a test, figure out how many different paths through the code execution can be taken. Compute the cyclomatic complexity of your function: Add one point to the cyclomatic complexity score for every if, for every case, for every boolean and for every loop (but not every loop thoruhg the loop). You're going to need at least points-many different cases, and enough assertions to check that the cases are correct.
If your cyclomatic complexisty is higher than 7, you should probably refactor the function.
The post generator's arguments are functionally equivalent to those of wp_insert_post
classTestFunctionsextendsWP_UnitTestCase {
functionsetUp() {
parent::setUp();
$this->post = $this->factory->post->create_and_get(array(
// these args are the same as in wp_insert_post// Here's how this works: https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/post.php#L3377'tax_input' => array(
# 'taxonomy' => 'term', // note: slug, not ID'series' => 'test'
)
));
}
// do your tests here
}
Generating multiple posts in a prominence
The post generator's arguments are functionally equivalent to those of wp_insert_post
classTestFunctionsextendsWP_UnitTestCase {
functionsetUp() {
parent::setUp();
$this->factory->post->create_many(array(
// these args are the same as in wp_insert_post// Here's how this works: https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/post.php#L3377'tax_input' => array(
# 'taxonomy' => 'term','series' => 'test'
)
));
}
// do your tests here
}
Creating posts in a category and querying that category
When creating the post, the category id must be in an array(), even if it is singular.
I tried this with wp_set_current_user, and it worked for running the test as that user, but all subsequent tests ran as that user. wp_set_current_user doesn't appear to be able to switch back to the default test-running, omniscient, omnipotent user.
Running a test on a different page
Unless otherwise specified, tests run on the homepage of your site, /. If you want to run tests on a different page on your site, such as a post, you can use the go_to() method of WP_UnitTestCase:
classRunTestsElsewhereextendsWP_UnitTestCase {
functionon_a_post() {
$post = $this->factory->post->create();
$this->go_to('/?p=' . $post ); // /?p=12345 is a guaranteed-to-work link// run your test
}
functionon_a_category() {
$category = $this-factory->category->create();
$post = $this->factory->post->create(array(
'post_category' => $category,
));
$this->go_to('/?cat=' . $category); // /?cat// run your test
}
}
great, thanks