Php 如何在Codeception验收测试中检查Wordpress插件是否激活

Php 如何在Codeception验收测试中检查Wordpress插件是否激活,php,wordpress,codeception,Php,Wordpress,Codeception,我正在学习如何在Wordpress中编写插件,我想测试插件在激活时的行为。我使用的是as-TDD框架。我打算使用模块WpWebDriver和Chrome编写一个验收测试 问题是我编写的测试相互干扰。出于这个原因,我需要在每次测试之前重置插件的状态,但我不知道怎么做 在每次测试之前,我需要: 测试插件是否处于活动状态 如果是,则将其停用 如果不是,那就什么也不做 我知道我可以在Cest类中使用\u before方法。这是我的密码: <?php class activationCest {

我正在学习如何在Wordpress中编写插件,我想测试插件在激活时的行为。我使用的是as-TDD框架。我打算使用模块WpWebDriver和Chrome编写一个验收测试

问题是我编写的测试相互干扰。出于这个原因,我需要在每次测试之前重置插件的状态,但我不知道怎么做

在每次测试之前,我需要:

  • 测试插件是否处于活动状态
  • 如果是,则将其停用
  • 如果不是,那就什么也不做
我知道我可以在Cest类中使用
\u before
方法。这是我的密码:

<?php
class activationCest
{
    public function _before(AcceptanceTester $I)
    {        
        // what to do here???
    }

    public function _after(AcceptanceTester $I)
    {
    }

    // tests
    public function activationCaseFailPHP(AcceptanceTester $I)
    {
        $I->wantTo('see an error message if the PHP version 
                    is not compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Your PHP version is outdated. 
                 Testwidget requires a PHP version equal or superior
                 to 7.0. Contact your hosting provider about 
                 how to update PHP');
    }

    public function activationCaseFailWP(AcceptanceTester $I)
    {
        $I->wantTo('see an error message if the Wordpress version
                    is not compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Your Wordpress version is outdated. 
                 Testwidget requires a Wordpress version equal 
                 or superior to 5.0. Contact your hosting provider
                 about how to update Wordpress');
    }

    public function activationCaseSuccess(AcceptanceTester $I)
    {
        $I->wantTo('see a success message if the PHP and Wordpress versions are 
                    compatible with the plugin');

        $I->loginAsAdmin();
        $I->amOnPluginsPage();
        $I->activatePlugin('testwidget');

        $I->see('Selected plugins activated', '#message');
    }
}
当然,它不起作用。程序抛出错误:
[error]对未定义函数的调用是\u plugin\u active()
,因为Wordpress函数不在范围内

Codeception的文档中说,有一些方法,比如
canseelement
cantSeeElement
,用于测试某个元素是否在页面上,如果失败则不会停止测试。正如您所看到的,Codeception操作似乎也有类似之处

我不知道这些步骤装饰器是否是一个解决方案,因为我不清楚它们是如何工作的,以及如何设置它们


你认为解决这个问题最简单的办法是什么?如果您处在我的位置,您将如何解决此问题?

验收测试无法执行任何服务器端代码,因此您无法使用wordpress功能,您只能使用WpWebDriver模块提供的帮助器方法与网站交互

由于
see
方法在看不到预期内容时抛出异常,所以只有在断言不满足时,您才能捕获该异常并执行另一个代码

public function _before(AcceptanceTester $I)
{
    $I->loginAsAdmin();
    $I->amOnPluginsPage();
    try {
        //it throws PHPUnit Assertion Failed exception if plugin is active.
        $I->seePluginDeactivated('my-plugin');
    } catch (\Exception $e) {
        $I->deactivatePlugin('hello-dolly');
    }
}
如果希望在许多测试文件中重用此代码,请将其作为帮助器方法,如中所述