Custom Report

With ScanAPI you can create your own report template to show the results the way you prefer! For that, the first step is to create a Jinja template file. The template can have the extension you prefer: .html, .md, .txt, .xml

Template Context

Inside you template, you have some variables and methods which you can access to have ScanAPI information.

project_name

The project_name defined in the Configuration File.

For example:

<header>
    {% if project_name %} {# when project_name is set in the config file #}
      <h1> Report generated for <span>{{ project_name }}</span></h1>
    {% else %} {# when project_name is not set in the config file #}
      <h1> Report generated for your API </h1>
    {% endif %}
</header>

now

The current datetime

For example:

<p> Report Generated at: {{now}}</p>

session

The object responsible to store the ScanAPI run information.

successes

The number of test_results that succeed in the session.

For example:

<span><strong>Number of PASSED:</strong> {{session.successes}}</span>

failures

The number of test_results that failed in the session.

For example:

<span><strong>Number of FAILURES:</strong> {{session.failures}}</span>

errors

The number of test_results that got an error in the session.

<span><strong>Number of ERRORS:</strong> {{session.errors}}</span>

exit_code

The exit code returned by the ScanAPI process.

<span>The script returned the exit code: <strong>{{session.exit_code}}</strong></span>

started_at

The datetime when ScanAPI started to run.

<span><strong>Started at: </strong> {{ session.started_at }}</span>

elapsed_time()

The elapsed time since the session started.

<span><strong>Total Time:</strong> {{ session.elapsed_time() }}</span>

results

A generator object containing results for each request. Each result contains the information for a request.

{% for result in results -%}
...
{% endfor %}

response

The Response object which contains the server’s response to the HTTP request.

{% for result in results -%}
  {% set response = result.response %}
  {% set request = response.request %}

  <p>Full URL: <a href="{{request.url}}"> {{request.url}}</a></p>
  <p>Response Status Code: <span>{{ response.status_code }}</span></p>
{% endfor %}

no_failure

A boolean that indicates if the tests for the request didn’t get any failure. True if there are no failures or errors. False if there is any failure or error.

{% for result in results -%}
  {% set endpoint_status_label = "PASS" if result.no_failure else "FAIL" %}

  <p>Status: <span>{{ endpoint_status_label }}</span></p>
{% endfor %}

tests_results

A list object containing the results for each test. Each result contains the information of a test for the request.

{% for result in results -%}
  {% set tests = result.tests_results %}

  {% for test in tests -%}
    ...
  {% endfor %}
{% endfor %}
name

The name of the test defined in the API specification.

{% for result in results -%}
  {% set tests = result.tests_results %}

  {% for test in tests -%}
    <p>Test Name: <span>{{ test.name }}</span></p>
  {% endfor %}
{% endfor %}
status

The status of the test result. One of the values: "passed", "failed or "error".

{% for result in results -%}
  {% set tests = result.tests_results %}

  {% for test in tests -%}
    <p>Test Name: <span>{{ test.name }}</span></p>
    <p>Test Status: <span>{{test.status|upper}}</span></p>
  {% endfor %}
{% endfor %}
failure

The assertion sentence that failed. It will be empty if there is no failure.

{% for result in results -%}
  {% set tests = result.tests_results %}

  {% for test in tests -%}
    <p>Test Name: <span>{{ test.name }}</span></p>
    <p>Test Status: <span>{{test.status|upper}}</span></p>
    {% if test.failure %}
      <span>{{test.failure}} is false</span>
    {% endif %}
  {% endfor %}
{% endfor %}
error

The exception thrown in the test. It will be empty if there is no error.

{% for result in results -%}
  {% set tests = result.tests_results %}

  {% for test in tests -%}
    <p>Test Name: <span>{{ test.name }}</span></p>
    <p>Test Status: <span>{{test.status|upper}}</span></p>
    {% if test.error %}
      <span>An error occurred: {{test.error}}</span>
    {% endif %}
  {% endfor %}
{% endfor %}

Running ScanAPI with Custom Report

After creating your report template, now you can run ScanAPI using it. For example:

$ scanapi run -t my_template.html

And that is it! Now ScanAPI will use you custom template my_template.html instead of the default one.

Also, if you want to check, this is the default template code, it might help you!