假设您使用的是WP\\U UnitTestCase,请在测试类中尝试以下操作:
private $post_id;
// use the factory to create a post with some fake data
public function setUp() {
parent::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;
setup_postdata($post);
// profit
$yourthing->function_you_test();
}