3

WordPress setup: Installed WordPress using VVV.

PHPUnit setup: I have setup the testing suite as mentioned in Pippin's blog. And accessing PHPUnit after logging into the box machine by using vagrant ssh.

Test case: I'm writing my test cases inside the /tests/ folder in the test-{my-plugin-filename}.php file.

function test_my_plugin_method() {
    $links = plugin_method();
}

Where I'm stuck?

When I run PHPUnit, I'm getting the following error.

Error: Call to undefined function plugin_method()

I have added the following line.

require_once( dirname( dirname( __FILE__ ) ) . '/plugin-file.php' );

But I believe I'm missing something. Any help would be much appreciated.

Edit 1:

I executed wp scaffold plugin-tests and then the following line as mentioned in Pippin's blog (refer above for link).

bash bin/install-wp-tests.sh wordpress_test root '' localhost latest

Finally I executed phpunit.

bootstrap.php

I haven't changed anything in the bootstrap.php file.

<?php

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir = '/tmp/wordpress-tests-lib';
}

require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
    require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';
1

Is the plugin_method() publicly available? Check whether it is a class method or belongs to a namespace.

If it belongs to a class, then you have to create an object of the class before you can call the method. Similarly if it is a namespace method, then you have to call the function with the full namespace.

  • Thanks @Sudar for the answer. The plugin_method is not publicly available and that's the reason why I was getting the error. – Maria Daniel Deepak Jul 5 '16 at 1:35
1

Can you show full code in your unit tests bootstrap file?

I am guessing the plugin would not be loaded properly, here is how it can be loaded as a mu-plugin when running tests.

Anyway, I would suggest you to use WP CLI's command wp scaffold plugin-tests to get started with tests quickly documented here.

  • Thanks for your comment. I have included the bootstrap.php code. – Maria Daniel Deepak Jun 30 '16 at 0:38
  • Please have relevant code in the answer instead of pointing people to external resource to read 4 lines of code. – Mark Kaplun Jun 30 '16 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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