module-rest icon indicating copy to clipboard operation
module-rest copied to clipboard

REST module prints complete response ("body" report)

Open manofearth opened this issue 6 years ago • 1 comments

Method \Codeception\Module\REST::_failed should take into account shortDebugResponse setting before adding "body" report. Otherwise test output becomes quite unreadable (in case of long responses) even if shortDebugResponse was set to true. Or maybe better create new setting for shortening "body" report (something like shortResponseBody: true). Another solution could be making \Codeception\Test\Metadata::$reports field write-accessible, to allow shortening it in \Codeception\Module::_failed hook

manofearth avatar Jan 23 '20 09:01 manofearth

Had the same Problem and found a quick solution so i thought i'd share it:

In the yml config for your suite(e.g. api.suite.yml):

        - \Helper\Api:
            shortResponseBody: 600

In the Helper Class for your suite(e.g. Api.php):

class Api extends \Codeception\Module
{

...

  public function _failed(\Codeception\TestInterface $test, $fail)
  {
    $body = $test->getMetadata()->getReports()['body'];
    $shortResponseBody = $this->_getConfig('shortResponseBody');

    if (isset($body) && isset($shortResponseBody) && strlen($body) > $shortResponseBody) {
      $start = substr($body, 0, $shortResponseBody / 2);
      $end = substr($body, -$shortResponseBody / 2);
      $newBody = $start . '\e ... \e' . $end;
      $test->getMetadata()->addReport('body', $newBody);
    }
  }

...

}

trenkmann avatar Feb 17 '20 18:02 trenkmann