Giter Club home page Giter Club logo

wordpress-plugin-tests's Introduction

WordPress Plugin Tests

This repo contains unit testing skeleton files designed for use in WordPress plugins that utilize WordPress's own unit testing framework and PHPUnit. We've outlined two methods of unit testing your WordPress plugin. First within a local installation of WordPress, and a second method using Travis CI.

Build Status

Installation

  1. Copy .travis.yml, phpunit.xml.dist, and the tests directory into the root folder of your plugin.
  2. Open tests/bootstrap.php and update the active_plugins setting to point to your main plugin file.

Running Unit Tests Locally

  1. Clone a copy of WordPress from this GitHub mirror of the official develop.svn.wordpress.org repository:

    git clone git://develop.git.wordpress.org/ wordpress

  2. Copy your plugin (along with unit testing files) into the copy of WordPress that was included in the clone above under: src/wp-content/plugins

  3. Copy the wp-tests-config-sample.php file in the root of the wordpress folder to wp-tests-config.php, and make the appropriate changes pointing it to a new, empty MySQL database it can use for testing. DO NOT USE A WORKING WORDPRESS DATABASE, IT WILL BE LOST!

  4. Run phpunit from your plugin's root folder.

Writing Unit Tests

Create all new test cases under the tests folder with filenames prefixed with test_. In those files, create a new class (name does not matter at all, but it's recommended to prefix class names with WP_Test_) that extends WP_UnitTestCase. All methods in this class prefixed with test_ will be run as unit tests. See the PHPUnit documentation for available assertions and other API available for writing tests.

An example has been provided at tests/test_wordpress_plugin_tests.php.

Configuring Travis CI

Using Travis CI to run your unit tests absolutely requires that your plugin is maintained on GitHub in a public repository. This will not work otherwise.

  1. Activate Travis CI for your plugin.
  2. The first test run needs to be triggered by a push to your plugin's GitHub repository after you have activated it in Travis CI.

Any git push to your plugin repository from here on out will automatically trigger new test runs on Travis CI.

You will likely want to customize .travis.yml to suite your plugin's needs in regards to compatible versions of PHP and WordPress.

Using Grunt? No problem.

Just add these commands to your before_script step in .travis.yml:

    - npm install -g grunt-cli
    - npm install

If you use the same method that WordPress does for adding a phpunit task to your plugin, then you can just use grunt test instead of phpunit for your script.

wordpress-plugin-tests's People

Contributors

benbalter avatar blobaugh avatar dexterthedragon avatar johnpbloch avatar lloc avatar scribu avatar tierra avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.