Testing plugin activation/installation and uninstall with the core WordPress test suite can be done more easily and properly with these additional utilities. Their main focus is testing uninstallation, but they test activation/installation as well. This is the example test case from there, and shows how you would test install and uninstall:
<?php
/**
* Test uninstallation.
*/
/**
* Plugin uninstall test case.
*
* Be sure to add "@group uninstall", so that the test will run only as part of the
* uninstall group.
*
* @group uninstall
*/
class My_Plugin_Uninstall_Test extends WP_Plugin_Uninstall_UnitTestCase {
//
// Protected properties.
//
/**
* The full path to the main plugin file.
*
* @type string $plugin_file
*/
protected $plugin_file;
//
// Public methods.
//
/**
* Set up for the tests.
*/
public function setUp() {
// You must set the path to your plugin here.
$this->plugin_file = dirname( dirname( __FILE__ ) ) . '/myplugin.php';
// Don't forget to call the parent's setUp(), or the plugin won't get installed.
parent::setUp();
}
/**
* Test installation and uninstallation.
*/
public function test_uninstall() {
/*
* First test that the plugin installed itself properly.
*/
// Check that a database table was added.
$this->assertTableExists( $wpdb->prefix . 'myplugin_table' );
// Check that an option was added to the database.
$this->assertEquals( 'default', get_option( 'myplugin_option' ) );
/*
* Now, test that it uninstalls itself properly.
*/
// You must call this to perform uninstallation.
$this->uninstall();
// Check that the table was deleted.
$this->assertTableNotExists( $wpdb->prefix . 'myplugin_table' );
// Check that all options with a prefix was deleted.
$this->assertNoOptionsWithPrefix( 'myplugin' );
// Same for usermeta and comment meta.
$this->assertNoUserMetaWithPrefix( 'myplugin' );
$this->assertNoCommentMetaWithPrefix( 'myplugin' );
}
}
I think this is probably what you are trying to achieve.
As far why your tests are failing as they are, I'm not sure. To debug it you need to see where that error is coming from in activate_plugin()
; what part of the check for the plugin file is failing? Tools like xdebug are invaluable for this sort of thing. If you don't have xdebug installed for the PHP build that you are using (you probably should get it), you'll have to debug it manually by looking at the source code of activate_plugin()
et al. and seeing where that error is originating.
assetNotWPError()
method seems to check if the result is an\WP_Error
instance (what you can check withis_wp_error()
). I would assume that you need to check if there's an\Exception
of any kind. Aside from that, I am not sure if you set up everything properly. Including some file normally should happen in some tear up step. You might want to exchange WP core unit test stuff with BrainMonkey from @gmazzap.