Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force test fail in Codeception

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!

like image 590
Serogia Avatar asked Aug 23 '15 12:08

Serogia


4 Answers

You can use the fail action on your actor. i.e.

$I->fail('this test fails... just because');
like image 190
Jim Maguire Avatar answered Nov 01 '22 10:11

Jim Maguire


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();
    }
}
like image 34
Bushikot Avatar answered Nov 01 '22 10:11

Bushikot


You can use PHPUnit\Framework\Assert::fail() or throw PHPUnit\Framework\AssertionFailedError exception.

like image 39
Ihor Harmider Avatar answered Nov 01 '22 09:11

Ihor Harmider


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.

like image 23
Greenkey Avatar answered Nov 01 '22 10:11

Greenkey