0

How do we use string replacements and variables in the tests? I suspect this is common and I simply do not know where to look. Example test:

$I = new ApiTester($scenario);
$I->wantTo('Get Stuff');
$I->amHttpAuthenticated('myuser', 'mypass');
$I->haveHttpHeader('Content-Type', 'application/json');
$I->haveHttpHeader('Custom-Header', 'friendlycorp');
$I->sendGet('/a/path/that/contains/variables');
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);

Each REST API location being tested uses different values, and also the URL changes depending on the service being tested. So, perhaps something like:

$I->amHttpAuthenticated('{USERNAME}', '{PASSWORD}');
$I->sendGet('/a/path/{SERVICE_ID}/and/{SOMETHING_ELSE}/and/such');
$I->haveHttpHeader('Custom-Header', '{SERVICE_ID}');

I'm hopeful that a configuration file could then define the likes of {SERVICE_ID} and {FUNCTION} depending on the REST API service location I'm testing, along with other variables. For example:

FriendlyCorp:
    SERVICE_ID: friendlycorp
    SOMETHING_ELSE: foo
    USERNAME: myuser
    PASSWORD: mypass
OpenExample:
    SERVICE_ID: openexample
    SOMETHING_ELSE: bar
    USERNAME: myuser2
    PASSWORD: mypass2

Then when executing codeception I'd choose to run tests against FriendlyCorp or OpenExample and said values would be used within the tests.

1 Answer 1

5

Use Cest format with Examples

 /**
  * @example { "service_id": "friendlycorp", "something_else": "foo", "username": "myuser", "password": "mypass" }
  * @example { "service_id": "openexample", "something_else": "bar", "username": "myuser2", "password": "mypass2" }
  */
  public function services(ApiTester $I, \Codeception\Example $example)
  {
    $I->amHttpAuthenticated($example['username'], $example['username']);
    $I->haveHttpHeader('Content-Type', 'application/json');
    $I->haveHttpHeader('Custom-Header', $example['service_id']);
    $I->sendGet("/a/path/{$example['service_id']}/and/{$example['something_else']}/and/such");
    $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.