I am making WebServices tests with Codeception, here is my code:
//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. i.e.
$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();
    }
}
                        You can use PHPUnit\Framework\Assert::fail() or throw PHPUnit\Framework\AssertionFailedError exception.
There is another problem with Codeception 4 and upper. 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. When you install it, this module must be added in config file, such as "api.suite.yml"
actor: ApiTester
modules:
  enabled:
    ...
    - \Codeception\Module\Asserts
After this, you need to rebuild codeception and this method appear to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With