• I’m trying to set up unit+integration testing for my plugin, and I’ve followed the instructions on the wordpress site, https://make.www.ads-software.com/cli/handbook/misc/plugin-unit-tests as well as many other tutorials that either use PHPUnit or Codeception+wp-browser (which uses PHPUnit under the hood) in order to start testing the plugin’s functions etc.

    My plugin uses custom tables, which are installed upon plugin activation if they aren’t present, and also some custom constants located in wp-config.php

    The general testing setup creates a new database/install for running the tests, however it also appears that the plugin is pre-activated, so the activation scripts never run and the tables aren’t installed. Also it doesn’t include any of the custom constants. Because of this obviously fatal errors get triggered.

    Is there a way to run activation scripts right after the test database gets installed, in order to ensure the tables are there? Also how can I include the custom constants (likely with testing values) when using PHPUnit?

    thanks!

Viewing 1 replies (of 1 total)
  • Hi, @nathanmal:

    For constants or defines (typically used in wp-config.php), how you use them in a particular test case will vary.

    For example, you could drop a define into your function, and then trigger your test. In this way you could have a test that ran its evaluation without the constant (fail), add the define, and then re-run the evaluation (succeed ????). Bundling cascading or either/or evaluations can keep your tests short and easy to read.

    Or you could use PHPUnit “fixtures” to control the state of the entire test case. Here you could set up a function that adds defines that might be used across the entire test case class. Or your setUp could activate or install your database! PHPUnit’s documentation goes pretty deep into fixtures and the neat features they offer.

    Regarding your activation function, you can just call that directly. For instance:

    public function test_activation() {
        $this->myplugin_activate(); //replace with what you are hooking via register_activation_hook
        //...check if things worked
    }

    Because the scope of the test is your plugin, and not WordPress itself, you don’t need to “test if WordPress has activated the plugin”. A key benefit is this also gives you direct control over when the activation is initiated, so you can use fixtures or mocks around that as needed.

Viewing 1 replies (of 1 total)
  • The topic ‘PHPUnit/Codeception + Plugin with custom tables & constants’ is closed to new replies.