Title: PHPUnit/Codeception + Plugin with custom tables &amp; constants
Last modified: December 26, 2021

---

# PHPUnit/Codeception + Plugin with custom tables & constants

 *  [nathanmal](https://wordpress.org/support/users/nathanmal/)
 * (@nathanmal)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/phpunit-codeception-plugin-with-custom-tables-constants/)
 * I’m trying to set up unit+integration testing for my plugin, and I’ve followed
   the instructions on the wordpress site, [https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests](https://make.wordpress.org/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)

 *  [Brian Alexander](https://wordpress.org/support/users/ironprogrammer/)
 * (@ironprogrammer)
 * [4 years, 4 months ago](https://wordpress.org/support/topic/phpunit-codeception-plugin-with-custom-tables-constants/#post-15230208)
 * Hi, [@nathanmal](https://wordpress.org/support/users/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.

## Tags

 * [phpunit](https://wordpress.org/support/topic-tag/phpunit/)
 * [plugin-development](https://wordpress.org/support/topic-tag/plugin-development/)
 * [testing](https://wordpress.org/support/topic-tag/testing/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Brian Alexander](https://wordpress.org/support/users/ironprogrammer/)
 * Last activity: [4 years, 4 months ago](https://wordpress.org/support/topic/phpunit-codeception-plugin-with-custom-tables-constants/#post-15230208)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
