I am trying to do unit test on codeception with wp-browser for a wordpress plugin based on a boilerplate.

The problem that is in this line of code I initialize the class for admin only when is not an AJAX request and is is_admin().

So I have the problem that I do the test but is not executed when is in the admin interface. I have that code that force to be an admin and in the admin interface:

wp_set_current_user( 1 );
set_current_screen( 'edit.php' );

but obviously is not running before the test itself (I put it on setUp) so I was thinking that the method in the test (class) was executed later (I guess) but the plugin is initialized before.

My idea was to do a plugin to load in wordpress before the all unit tests with this two lines of code but I think that is only an hack.

Do you have any suggestions?

1 Answer 1


There are basically three options:

  1. Load that file with the admin-side code on-the-fly in setUp() or setUpBeforeClass(), so it will be loaded when those tests run.
  2. Load that file before the test suite is loaded and run, like in a bootstrap.php file or something, so it will be loaded for all the tests.
  3. Separate your tests into several different test suites, some that run as if is_admin(), and some that do not. (Or, always run the tests as if is_admin(), similar to your suggestion with using a plugin.)

Of these three, I have done both of the first two when testing my plugins. #2 is really the easiest, since you just load that file once and then forget it, you don't have to make sure that every testcase that relates to the code from that file included the file in setUpBeforeClass().

The only downside to #1 and #2, is that they circumvent your plugin's logic that normally determines when this code is loaded. So in your tests you are testing the code in that file, but you aren't testing that your plugin is actually loading that file properly.

This is OK, if we are talking about unit tests. Unit tests are about testing the individual units of your code, like each of the different aspects of each function contained in that file. They aren't really about ensuring that your plugin operates properly as a whole. Since you are using Codeception, you can also run functional and acceptance tests, in addition to unit tests. In your function and acceptance tests is where you will test that your plugin operates properly as a whole, which includes (indirectly) testing that that admin-side code is loaded in the admin.

So, I would just load the file in setUpBeforeClass() or in bootstrap.php or the like, and not worry about this in my unit tests. In your acceptance tests, when you test how your plugin actually operates within the WordPress dashboard via the browser, you will be testing that the admin-side code is being loaded when it should.

It might be possible to do #3 for the unit tests, but basically your acceptance tests are already doing this: when you have the browser visit your plugin's UI within the admin to test it, your test will fail if the admin-side code isn't being loaded when it should.

  • Thanks for the suggestions! I think that the 1 or 2 are perfect, I have only to test what is the best. I do also get_current_screen in the code in few piece (used for enqueue styles) so I have to do few tests. For the unit test, sure every class of my plugin will have specific unit test so admin and frontend will be not go in conflict.
    – Mte90
    Commented Dec 19, 2016 at 21:58
  • 1 with the including of the file fixed everything :-D
    – Mte90
    Commented Dec 23, 2016 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.