[英]How to force test fail in Codeception
I am making WebServices tests with Codeception, here is my code:我正在使用 Codeception 进行 WebServices 测试,这是我的代码:
//Making first query for getting needed parameter
$I->wantTo('Make something');
$I->sendPOST($this->route, [
'token' => Fixtures::get('token'),
'id' => Fixtures::get('some_id')
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('"error_message"');
//Then I get what I need with regexp
if (preg_match('/^.*\s(?<value_i_need>\d+)\.$/', $I->grabDataFromResponseByJsonPath('$.status.error_message')[0], $matches)) {
$I->sendPOST($this->route, [
'token' => Fixtures::get('token'),
'id' => Fixtures::get('some_id')
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('"something"');
$I->seeResponseContains('"something_else"');
} else {
//And if I don't get needed parameter with regular expression, here I have to force test fail
}
Does anybody know how to force test fail?有人知道如何强制测试失败吗?
Thanks in advance!提前致谢!
You can use the fail action on your actor.您可以对您的演员使用失败操作。 ie
IE
$I->fail('this test fails... just because');
You can mark your test as skipped or incomplete.您可以将测试标记为已跳过或未完成。 Here is example:
这是示例:
public function tryToMakeAwesomeThings(ApiTester $I, Scenario $scenario)
{
if ( ... ) {
// do something
} elseif ( ... ) {
$scenario->incomplete();
} elseif ( ... ) {
$scenario->skip();
}
}
您可以使用PHPUnit\\Framework\\Assert::fail()
或抛出PHPUnit\\Framework\\AssertionFailedError
异常。
Use assertInstanceOf()
from https://codeception.com/docs/modules/Asserts使用来自https://codeception.com/docs/modules/Asserts 的
assertInstanceOf()
Example:例子:
public function test_array_result() {
/** @var Demo $demo */
$demo = Common::get_which_class( $this->post_id );
self::assertInstanceOf( Demo::class, $demo, 'Unable to instantiate the Demo class.');
$x = [ 'a', 'b', 'c' ];
$this->assertEqualSetsWithIndex( $x, $demo->get_result() );
}
There is another problem with Codeception 4 and upper. Codeception 4 及更高版本还有另一个问题。 Method fail now in Assertion module https://codeception.com/docs/modules/Asserts This module not included in codeception by default, you must install it separately.
Method fail now in Assertion 模块https://codeception.com/docs/modules/Asserts该模块默认不包含在 codeception 中,您必须单独安装。 When you install it, this module must be added in config file, such as "api.suite.yml"
安装时,必须在配置文件中添加该模块,如“api.suite.yml”
actor: ApiTester
modules:
enabled:
...
- \Codeception\Module\Asserts
After this, you need to rebuild codeception and this method appear to use.在此之后,您需要重建 codeception,并且这种方法似乎可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.