Opened 10 months ago
Last modified 5 months ago
#21271 new enhancement
Make admin backend unit-tests friendly
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Administration | Version: | 3.4.1 |
| Severity: | normal | Keywords: | needs-patch dev-feedback |
| Cc: | scribu, mikeschinkel@…, daxitude |
Description
Recently I started writing unit tests for my plugins. Beside tests focused on functionality (where I directly call my plugin functions) I write some basic integrations tests, which should test that WordPress will call my function, pass data in expected format and recognize data returned from it. For frontend it is quite easy - theme API is well-defined, so I can write something like this:
public function test_something() {
// add new post
$post_id = wp_insert_post( array( ... ) );
$this->assertGreaterThan( 0, $post_id );
// go to post page
$this->go_to( get_permalink( $post_id ) );
// main loop
$checked_post = false;
while ( have_posts() ) {
the_post();
if ( $post_id == get_the_ID() ) {
$checked_post = true;
// test that content is modified
ob_start();
the_content();
$result = ob_get_clean();
$this->assertEquals( '...', $result );
}
}
// make sure test above was executed
$this->assertTrue( $checked_post );
}
Unfortunately this is not true for admin backend - there most of code is written directly at file level (not in functions and classes), so I would need to either duplicate this code in my tests (bad approach, because would have to monitor original code for changes), or test using whole file (either load it directly or use Selenium) - in this case test would be more complicated.
Therefore I logged this ticket, to start discussion how we can perform refactoring of admin backend to make it more tests-friendly, and how to test it more thoroughly. Most probably we would also need to modify the testing framework (e.g. introduce new admin_go_to() method).

Some non-admin page would also benefit from such refactoring, e.g. wp-login.php.