Articles of unit tests

Unit testing wordpress plugins with plugin options

I’m new with unit testing in WordPress. I managed to setup the unit testing environment using PHP Unit and WordPress tests but I am having trouble accessing the plugin options that I’ve set in the plugin. class nrw_pg{ private $twitter_id; public function __construct(){ $options = get_option(‘nrw_pg_options’); $this->twitter_id = (!empty($options[‘nrw_pg_options_twitter_id’])) ? $options[‘nrw_pg_options_twitter_id’] : ”; } public […]

Is there any way of of making an admin-ajax request without the use of die()?

Pretty much what the title says. I have a custom plugin written, that relies on the use of admin-ajax to handle various forms and actions. That’s all working fine, however as the various functions echo out responses I have to throw in a die() after the echo function. ie: echo $return_string; die(); All fine, however […]

How to add an image for unit testing?

I have to test the function where it calls wp_get_attachment_image_src. How do I add an image for testing, because right now if I call this method it returns false.

Setting Up PHPUnit tests for WP development on Windows

I’ve been following the instructions in this article to try to set up some testing. I got as far as this instruction bash bin/install-wp-tests.sh wordpress_test root ‘mypass’ localhost latest before I hit some problems. I have git bash installed so the command ran, but I got this error when I runphpunit: Fatal error: require_once(): Failed […]

Theme unit test data Foreign fonts come in garbled on my machine

Having just tried out the Theme Unit Test Data from the WordPress.org Codex, I noticed that some of the body text doesn’t import properly on my set up which is a Mac running Mavericks. The effect is particularly noticeable on the Lorem ipsum page where non latin characters just arent rendering at all being replaced […]

Unit Testing a Plugin functionality with cron

I’m currently on the process of unit testing a plugin using the WordPress unit testing framework. In one of the functions to be tested,it will send mails to users in a periodic time interval so it means that the PHP script will need to be executed at a set time, say every 2 minutes. Is […]

How to properly test a method that is called by an action hook

The constructor in my class adds an action to the ‘init’ action hook: class My_Custom_Post_Type { function __construct( $type ) { $this->type = $type; add_action( ‘init’, array( $this, ‘register_my_type’ ); } function register_my_type() { register_post_type( $this->type); } } I was trying to test this in a unit test and finally realized that there was no […]

How can I properly set up dependencies in automated testing?

I’m trying to set up automated tests for my WP- and BP-dependent plugin, as described in: https://codex.buddypress.org/developer/automated-testing/ https://make.wordpress.org/core/handbook/automated-testing/ https://codex.buddypress.org/developer/automated-testing/writing-automated-tests-for-buddypress-dependent-plugins/ And so I’ve initialized a testing framework by: running wp scaffold plugin-tests adding lines to check out BP from svn to bin/install-wp-tests.sh loading BP with the loader described in the above manual page But I can’t […]

Install and load additional plugins when running unit tests

I want to write unit tests for a plugin. I have used WP-CLI to scaffold the test WordPress instance and can successfully run tests. The plugin I’m writing unit tests for is a site specific plugin rather than a stand-alone plugin destined for the wordpress.org repository. Being a site specific plugin, some functions use functions […]

unit testing admin password

I’m writing up unit tests for my plugin and I need a logged in user to test, but I can’t find the admin password anywhere. Does anybody know what the default admin password is? I’m extending the WP_UnitTestCase and taking the tests from https://unit-tests.svn.wordpress.org/trunk. My code at the moment: <?php require_once(‘myPlugin.php’); class MyPluginTest extends WP_UnitTestCase{ […]