bdvs/wordpress-integration-tester

Non-user / UI based integration testing for WordPress

dev-master 2021-07-16 09:23 UTC

This package is auto-updated.

Last update: 2024-11-16 23:54:15 UTC


README

An automated integration testing framework for WordPress.

Why?

When creating WordPress plugins, there are two types of testing:

  • Unit testing - generally with PHPUnit
  • Integration testing - either manually, using Selenium or some other tool for testing user based interactions

However, there's a third case that sits between the two:

  • Unit testing, but when your functions rely on data being present in the database either as a result of user interaction, or more commonly, data that has been placed there by another 3rd party plugin, perhaps based on settings and content entered via the WordPress CMS.

Enter WordPress Integration Tester - allowing automated integration testing of WordPress plugins using data that is present in the DB.

Installation

Via Composer: composer required bdvs/wordpress-integration-tester:dev-master

(NB. only dev-master available while in development)

Dependencies

  • WP CLI

Getting Started

  1. Create a new plugin to house your tests, or add the package to your plugin directly
  2. Create a test class that extends the WIT Test class
  3. Run $ wp wit run in the terminal
  4. View your results

Create a test class that extends the WIT Test class

/**
*
* My first WIT Test Class
*
**/

class My_First_WIT_Test_Class extends WIT_Test {
  
  
  /**
  *
  * Run set up before each test
  *
  * @param void
  * @return void
  *
  **/
  
  protected function __setup() {
  
    //do stuff before tests start
    //note that functions that start with __ are not considered test functions
    
    parent::__setup();
    
  }
  
  
  /**
  *
  * Close down and reset post-test
  *
  * @param void
  * @return void
  *
  **/
  
  protected function __close_down() {
  
    //do stuff when tests are done
    //note that functions that start with __ are not considered test functions
    
    //e.g. reload the database as it was before you ran tests...
    $this->__load_db( plugin_dir_path( __FILE__ ) . 'dbs/test-db.sql' );
    parent::__close_down();
    
  }


  /**
  *
  * My first WIT Test
  *
  * @param void
  * @return void
  *
  **/
  
  public function my_first_wit_test() {
  
    $this->__are_equal( array(
      'expected' => true, 
      'actual' => true, 
      'description' => 'Tests that pass show a pass in the console',
    ));
  
    $this->__are_equal( array(
      'expected' => true, 
      'actual' => false, 
      'description' => 'Tests that fail show a fail in the console',
    ));
    
  }

}

Run wp wit run in the terminal and view the results

With the above class in place and included, running $ wp wit run in the terminal will output a table like this:

Class Test Success
My_First_WIT_Test_Class Tests that pass show a pass in the console pass
My_First_WIT_Test_Class Tests that fail show a fail in the console fail

Contributing

If you are interested in contributing to the project or have any issues or feedback please raise an issue on this repo.