├── .travis.yml ├── phpunit.xml.dist ├── readme.md └── tests ├── bootstrap.php └── test_wordpress_plugin_tests.php /.travis.yml: -------------------------------------------------------------------------------- 1 | # Travis CI Configuration File 2 | 3 | # Tell Travis CI we're using PHP 4 | language: php 5 | 6 | # PHP version used in first build configuration. 7 | php: 8 | - "5.5" 9 | 10 | # WordPress version used in first build configuration. 11 | env: 12 | - WP_VERSION=master 13 | 14 | # Next we define our matrix of additional build configurations to test against. 15 | # The versions listed above will automatically create our first configuration, 16 | # so it doesn't need to be re-defined below. 17 | 18 | # WP_VERSION specifies the tag to use. The way these tests are configured to run 19 | # requires at least WordPress 3.8. Specify "master" to test against SVN trunk. 20 | 21 | # Note that Travis CI supports listing these above to automatically build a 22 | # matrix of configurations, but we're being nice here by manually building a 23 | # total of four configurations even though we're testing 4 versions of PHP 24 | # along with 2 versions of WordPress (which would build 8 configs otherwise). 25 | # This takes half as long to run while still providing adequate coverage. 26 | 27 | matrix: 28 | include: 29 | - php: "5.3" 30 | env: WP_VERSION=master 31 | - php: "5.4" 32 | env: WP_VERSION=3.8 33 | - php: "5.2" 34 | env: WP_VERSION=3.8 35 | 36 | # Clones WordPress and configures our testing environment. 37 | before_script: 38 | - export PLUGIN_SLUG=$(basename $(pwd)) 39 | - git clone https://github.com/tierra/wordpress.git /tmp/wordpress 40 | # - git clone . "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG" 41 | - cd .. 42 | - mv $PLUGIN_SLUG "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG" 43 | - cd /tmp/wordpress 44 | - git checkout $WP_VERSION 45 | - mysql -e "CREATE DATABASE wordpress_tests;" -uroot 46 | - cp wp-tests-config-sample.php wp-tests-config.php 47 | - sed -i "s/youremptytestdbnamehere/wordpress_tests/" wp-tests-config.php 48 | - sed -i "s/yourusernamehere/travis/" wp-tests-config.php 49 | - sed -i "s/yourpasswordhere//" wp-tests-config.php 50 | - cd "/tmp/wordpress/src/wp-content/plugins/$PLUGIN_SLUG" 51 | 52 | script: phpunit 53 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests 6 | 7 | 8 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | WordPress Plugin Tests 2 | ====================== 3 | 4 | This repo contains unit testing skeleton files designed for use in WordPress 5 | plugins that utilize WordPress's own unit testing framework and 6 | [PHPUnit](https://github.com/sebastianbergmann/phpunit/). We've outlined two 7 | methods of unit testing your WordPress plugin. First within a local installation 8 | of WordPress, and a second method using [Travis CI](http://travis-ci.org/). 9 | 10 | Installation 11 | ------------ 12 | 13 | 1. Copy `.travis.yml`, `phpunit.xml.dist`, and the `tests` directory into the 14 | root folder of your plugin. 15 | 2. Open `tests/bootstrap.php` and update the `active_plugins` setting to point 16 | to your main plugin file. 17 | 18 | Running Unit Tests Locally 19 | -------------------------- 20 | 21 | 1. Clone a copy of WordPress from this GitHub mirror of the official 22 | develop.svn.wordpress.org repository: 23 | 24 | ```git clone https://github.com/tierra/wordpress.git``` 25 | 26 | 2. Copy your plugin (along with unit testing files) into the copy of WordPress 27 | that was included in the clone above under: `src/wp-content/plugins` 28 | 3. Copy the `wp-tests-config-sample.php` file in the root of the `wordpress` 29 | folder to `wp-tests-config.php`, and make the appropriate changes pointing 30 | it to a new, empty MySQL database it can use for testing. DO NOT USE A 31 | WORKING WORDPRESS DATABASE, IT WILL BE LOST! 32 | 4. Run `phpunit` from your plugin's root folder. 33 | 34 | Writing Unit Tests 35 | ------------------ 36 | 37 | Create all new test cases under the `tests` folder with filenames prefixed with 38 | `test_`. In those files, create a new class (name does not matter at all, but 39 | it's recommended to prefix class names with `WP_Test_`) that extends 40 | `WP_UnitTestCase`. All methods in this class prefixed with `test_` will be run 41 | as unit tests. See the [PHPUnit documentation](http://www.phpunit.de/manual/current/en/) 42 | for available assertions and other API available for writing tests. 43 | 44 | An example has been provided at `tests/test_wordpress_plugin_tests.php`. 45 | 46 | Configuring Travis CI 47 | --------------------- 48 | 49 | Using Travis CI to run your unit tests absolutely requires that your plugin 50 | is maintained on GitHub in a public repository. This will not work otherwise. 51 | 52 | 1. [Activate Travis CI](http://travis-ci.org/profile) for your plugin. 53 | 2. The first test run needs to be triggered by a push to your plugin's GitHub 54 | repository after you have activated it in Travis CI. 55 | 56 | Any git push to your plugin repository from here on out will automatically 57 | trigger new test runs on Travis CI. 58 | 59 | You will likely want to customize `.travis.yml` to suite your plugin's needs in 60 | regards to compatible versions of PHP and WordPress. 61 | 62 | ### Using Grunt? No problem. 63 | 64 | Just add these commands to your `before_script` step in `.travis.yml`: 65 | 66 | ``` 67 | - npm install -g grunt-cli 68 | - npm install 69 | ``` 70 | 71 | If you use the same method that WordPress does for 72 | [adding a phpunit task](http://core.trac.wordpress.org/browser/trunk/Gruntfile.js) 73 | to your plugin, then you can just use `grunt test` instead of `phpunit` for your 74 | `script`. 75 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | array( 'plugin-slug/main-plugin-file.php' ), 13 | ); 14 | 15 | // If the develop repo location is defined (as WP_DEVELOP_DIR), use that 16 | // location. Otherwise, we'll just assume that this plugin is installed in a 17 | // WordPress develop SVN checkout. 18 | 19 | if( false !== getenv( 'WP_DEVELOP_DIR' ) ) { 20 | require getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/bootstrap.php'; 21 | } else { 22 | require '../../../../tests/phpunit/includes/bootstrap.php'; 23 | } 24 | -------------------------------------------------------------------------------- /tests/test_wordpress_plugin_tests.php: -------------------------------------------------------------------------------- 1 | assertTrue( true ); 16 | 17 | } 18 | 19 | /** 20 | * If these tests are being run on Travis CI, verify that the version of 21 | * WordPress installed is the version that we requested. 22 | * 23 | * @requires PHP 5.3 24 | */ 25 | function test_wp_version() { 26 | 27 | if ( !getenv( 'TRAVIS' ) ) 28 | $this->markTestSkipped( 'Test skipped since Travis CI was not detected.' ); 29 | 30 | $requested_version = getenv( 'WP_VERSION' ) . '-src'; 31 | 32 | // The "master" version requires special handling. 33 | if ( $requested_version == 'master-src' ) { 34 | $file = file_get_contents( 'https://raw.github.com/tierra/wordpress/master/src/wp-includes/version.php' ); 35 | preg_match( '#\$wp_version = \'([^\']+)\';#', $file, $matches ); 36 | $requested_version = $matches[1]; 37 | } 38 | 39 | $this->assertEquals( get_bloginfo( 'version' ), $requested_version ); 40 | 41 | } 42 | 43 | /** 44 | * Ensure that the plugin has been installed and activated. 45 | */ 46 | function test_plugin_activated() { 47 | 48 | $this->assertTrue( is_plugin_active( 'plugin-slug/main-plugin-file.php' ) ); 49 | 50 | } 51 | 52 | } 53 | --------------------------------------------------------------------------------