如何在Codeception功能测试中使用PHPUnit断言方法?
我正在使用Codeception对我的Laravel4PHP应用程序进行单元测试、功能测试和验收测试 我的单元测试如下所示:如何在Codeception功能测试中使用PHPUnit断言方法?,phpunit,codeception,Phpunit,Codeception,我正在使用Codeception对我的Laravel4PHP应用程序进行单元测试、功能测试和验收测试 我的单元测试如下所示: use Codeception\Util\Stub; class ExampleTest extends \Codeception\TestCase\Test { public function testExample() { $example = true; $this->assertSame($example, true); } } 我的功能测
use Codeception\Util\Stub;
class ExampleTest extends \Codeception\TestCase\Test
{
public function testExample()
{
$example = true;
$this->assertSame($example, true);
}
}
我的功能测试如下所示:
use \TestGuy;
class ExampleCest
{
public function example(TestGuy $I)
{
$I->amOnPage('/auth/login');
$I->see('Sign in');
}
}
但我也希望在功能测试中使用PHPUnit断言方法。但当我尝试时,我会出现以下错误:
调用未定义的方法ExampleCest::assertSame()
如何在Codeception功能测试中使用PHP断言方法?
\PHPUnit\u Framework\u assert::assertSame()
另一个解决方法是在测试套件中使用Helper方法
例如,对于assertSame()
方法
class ExpectedHelper extends \Codeception\Module
{
protected $test;
function _before(\Codeception\TestCase $test) {
$this->test = $test;
}
function assertSame($expected, $actual, $message = '')
{
$this->test->assertSame($exception, $actual, $message);
}
}
其中,ExpectedHelper是测试套件帮助程序名称(例如:UnitHelper,FunctionalHelper),应位于\u support文件夹下
您可以在测试中使用它作为
$I->assertSame('12340','12340')代码>因为Codeception 2.1(而不是2.0),您可以像其他断言一样使用它:
$I->assertSame($expected, $actual, $message);
但不要忘记在配置中启用断言
模块-例如:
class_name: UnitTester
modules:
enabled: [ Asserts ]
请注意:升级到2.1时,您可能需要更改配置-请参阅Codeception 4中的升级说明:只需添加断言模块:
modules:
enabled:
- \Codeception\Module\Asserts
这是关于“如何在功能测试中使用断言方法”问题的最佳答案。为了查看IDE中的其他方法,您必须在配置更改后运行一次测试套件。在codeception文档中:“默认情况下,codeception会在套件配置的每次更改中自动重建操作特征。”并且不要忘记“composer require--dev codeception/module asserts”