WordPress Unit Testing – Cannot Create Tables

You’ve just discovered an important feature of the core test suite: it forces any tables created during the test to be temporary tables. If you look in the WP_UnitTestCase::setUp() method you’ll see that it calls a method called start_transaction(). That start_transaction() method starts a MySQL database transaction: function start_transaction() { global $wpdb; $wpdb->query( ‘SET autocommit … Read more

WordPress test environment on windows

Your best bet to set up a reusable test environment on a Windows machine is to use Vagrant with a setup such as this. Vagrant will allow you to build portable, reusable virtual machines with a very simple interface which you can use across different operating systems and team members. To set it up, you … Read more

How to set up a user inside unit tests

Just few notes: Looks like you’re doing functional/integration tests rather than unit tests in isolation. Here you’re creating a user, but not setting it as the current one: $user = $this->factory->user->create(); wp_set_current_user( 1 ); You probably want: $user_id = $this->factory->user->create(); wp_set_current_user( $user_id ); Note that cookies aren’t set here, because of: tests_add_filter( ‘send_auth_cookies’, ‘__return_false’ ); … Read more

WordPress Themes and PHP unit

When it comes to unit testing themes, it’s a small jungle. I read on Make WordPress Core that things are changing (http://make.wordpress.org/core/2013/08/06/a-new-frontier-for-core-development/). I searched for blogs linking to that specific post in hope to find some useful. Found this: http://ben.lobaugh.net/blog/84669/how-to-add-unit-testing-and-continuous-integration-to-your-wordpress-plugin that looks promising. Note that it focuses on plugin testing, but useful for theme unit … Read more

Proper unit testing in WordPress

I think that what you are looking for would actually technically be called functional testing, integration testing, or acceptance testing. It sounds like you want to test the behavior of the front-end of your website (or the front-end behavior of a plugin or theme), not each unit of the code itself. You might use a … Read more