【发布时间】:2019-09-19 20:51:21
【问题描述】:
我正在学习如何在 Wordpress 中编写插件,我想测试插件在激活时的行为。我使用Codeception/Wp-Browser 作为 TDD 框架。我打算使用带有 Chrome 的 WpWebDriver 模块编写验收测试。
问题是我编写的测试相互干扰。出于这个原因,我需要在每次测试之前重置插件的状态,但我不知道该怎么做。
在每次测试之前我需要:
- 测试插件是否处于活动状态
- 如果是,则将其停用
- 如果不是,则什么也不做
我知道我可以在 Cest 类中使用 _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');
}
}
天真,我试过这个
public function _before(AcceptanceTester $I)
{
if (is_plugin_active('testwidget')) {
$I->deactivatePlugin('testwidget');
}
}
当然,它没有用。程序抛出错误:[Error] Call to undefined function is_plugin_active(),因为 Wordpress 函数 is_plugin_active 不在作用域内。
Codeception 的文档说有conditional assertions——即canSeeElement 或cantSeeElement 之类的方法,用于测试元素是否在页面上,如果失败则不会停止测试。 Codeception 操作似乎也有类似的东西,如您所见here。
我不知道这些步骤装饰器是否可以成为解决方案,因为我不清楚它们是如何工作的以及如何设置它们。
您认为解决此问题最简单的方法是什么?如果你处于我的位置,你会如何解决它?
【问题讨论】:
标签: php wordpress codeception