Title: Running WordPress tests with PHPUnit
Last modified: August 19, 2016

---

# Running WordPress tests with PHPUnit

 *  [dvmorris](https://wordpress.org/support/users/dvmorris/)
 * (@dvmorris)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/)
 * Has anyone ever gotten the wordpress test suite () to run using PHPUnit from 
   the command line, instead of running “php wp-test.php”?
 * I would like to integrate testing into a continuous integration environment, 
   and I am having trouble translating wp-test.php into a PHPUnit bootstrap file.
   I would like to run the WordPress test suite like this:
 * `phpunit --bootstrap bootstrap.php --log-junit phpunit.xml wp-testcase/test_actions.
   php`
 * I created a bootstrap.php file that is heavily based on wp-test.php, and I when
   I run it, I get this error:
 * `Fatal error: Call to undefined method stdClass::set_prefix() in C:\wamp\www\
   wordpress\trunk\src\wp-settings.php on line 286`
 * Line 286 of wp-settings.php is this:
 * `$prefix = $wpdb->set_prefix($table_prefix); // set up global tables`
 * It appears that $wpdb is not being defined properly by this point in the execution,
   but when I run “php wp-test.php”, which is almost the same execution steps, it
   works. It leads me to believe that there is some issue with the way PHPUnit bootstrap
   files allow or don’t play nice with all of WordPress’ global variables, like 
   $wpdb. I cannot change the WordPress source code to make it bootstrappable, so
   I thought I’d check here first and see if there’s a setting or flag I’m missing
   when running PHPUnit.
 * Any help would be greatly appreciated. Thanks,
 * Dave Morris

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [evomase](https://wordpress.org/support/users/evomase/)
 * (@evomase)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/#post-1551511)
 * Hi Dave,
 * I found the fix to your problem. I added the following lines below in my bootstrap.
 *     ```
       define( 'DOING_AJAX', true );
   
       global $wpdb;
       global $wp_embed;
   
       $GLOBALS[ '_wp_deprecated_widgets_callbacks' ] = array();
   
       require_once( 'wp-load.php' );
       ```
   
 * Also amended wp-settings.php and replaced
 *     ```
       $wp_widget_factory =& new WP_Widget_Factory();
       ```
   
 * with
 *     ```
       $GLOBALS[ 'wp_widget_factory' ] =& new WP_Widget_Factory();
       ```
   
 * Also you’ll need to add the code below in your PHPUnit_Framework_TestCase or 
   PHPUnit_Framework_TestSuite child class
 *     ```
       protected $backupGlobals = FALSE;
       protected $backupStaticAttributes = FALSE;
       ```
   
 * Hope that helps!
 * David Ogilo
 *  [romulodl](https://wordpress.org/support/users/romulodl/)
 * (@romulodl)
 * [15 years ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/#post-1551565)
 * Hello all,
 * I’m trying to do the same thing as Dave and I followed yours steps (thanks for
   that), but i’m getting this error when I run the PHPUnit command:
 *     ```
       PHP Fatal error:  Call to a member function add_rewrite_tag() on a non-object in /path/to/blog/wp-includes/taxonomy.php on line 331
       ```
   
 * And the line 331 of taxonomy.php is with this:
 *     ```
       $wp_rewrite->add_rewrite_tag("%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
       ```
   
 * I think the $wp_rewrite have the same problem that $wpdb had. I tried to declare
   this variable as global too but had no effect.
 * Could someone help me? Thank you.
 * Romulo De Lazzari
 *  [romulodl](https://wordpress.org/support/users/romulodl/)
 * (@romulodl)
 * [15 years ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/#post-1551566)
 * I’ve already solved the problem changing this line:
 *     ```
       $wp_rewrite =& new WP_Rewrite();
       ```
   
 * With this one:
 *     ```
       $GLOBALS['wp_rewrite'] =& new WP_Rewrite();
       ```
   
 * In wp-settings.php
 *  [romulodl](https://wordpress.org/support/users/romulodl/)
 * (@romulodl)
 * [15 years ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/#post-1551567)
 * In addiction, if you want to run an unit test in a WordPress with MultiSite enabled,
   you have to add this before including wp-load.php:
 *     ```
       define('WP_INSTALLING', 1);
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Running WordPress tests with PHPUnit’ is closed to new replies.

## Tags

 * [phpunit](https://wordpress.org/support/topic-tag/phpunit/)
 * [tests](https://wordpress.org/support/topic-tag/tests/)
 * [wpmu](https://wordpress.org/support/topic-tag/wpmu/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [romulodl](https://wordpress.org/support/users/romulodl/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/running-wordpress-tests-with-phpunit/#post-1551567)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
