Test in batch in your CI

Write your test scenarios once, then run them all in one call and see at once which ones fail. You keep the scenarios in a file of your repository, the platform runs them, up to 50 at a time, and a short script fails your pipeline when the bot does not answer as expected.

Important: the platform does not judge the answers. It returns what happened (who answered, how the conversation ended) next to the expected result you wrote, and your script compares them.

Before you start

  • A token of an application with evaluation:read and evaluation:write, and the variables BASE and TOKEN: see "Before you start" and step 1 of Your first test in 5 minutes.
  • For each scenario, the use case that should answer. Its number is in the address of the use case in the bot builder (…/use_cases/5190), or in handled_by.use_case_id when you send the question once.
  • When to run the tests. The bot changes in the ViaSay dashboard, not in your repository: run the tests on a schedule (every night), after each publication, or when your own systems change.

Step 1: Write your scenarios in a file

Save the file below as dataset.json in your repository. Each scenario is a conversation: the user's messages, and in metadata.expected what you expect, the number of a use case or "ai_assistant" for Louis.

{
  "name": "Bookings",
  "defaults": { "target": { "type": "bot" }, "version": "draft" },
  "scenarios": [
    {
      "name": "general question",
      "messages": [ { "text": "What are your opening hours?" } ],
      "metadata": { "expected": "ai_assistant" }
    },
    {
      "name": "change dates",
      "messages": [ { "text": "I want to change the dates of my booking" } ],
      "metadata": { "expected": 5190 }
    }
  ]
}

The table below describes each field of the file.

FieldContent
namethe name of the dataset, required
defaultsthe target and the version of the scenarios that do not set them. variables and metadata are merged with those of each scenario
scenarios1 to 1,000 scenarios, each with a name
scenarios[].messages1 to 100 messages: text, click, postback or file, as in a simulation
scenarios[].metadatafree: the platform keeps it and gives it back with the result

Send the file to create the dataset, the list of scenarios kept by the platform:

DS=$(curl -s -X POST "$BASE/v1/evaluation/datasets" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d @dataset.json | jq -r .dataset_id)
echo $DS

It prints the identifier of the dataset, for example ds_0489a7cd…. The use case of each target is checked now: an unknown one answers 422 unknown_use_case. Create the dataset once. After a change to the file, replace it with the same command and -X PUT "$BASE/v1/evaluation/datasets/$DS": unchanged scenarios keep their scenario_id. A scenario whose last simulation in a run ended completed cannot be changed or removed (409 scenario_completed): add a new scenario instead.

Step 2: Start a run

A run plays every scenario of the dataset.

RUN=$(curl -s -X POST "$BASE/v1/evaluation/runs" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{ \"dataset_id\": \"$DS\", \"concurrency\": 10 }" | jq -r .run_id)
echo $RUN
  • concurrency: how many scenarios play at the same time, 1 to 50, 10 by default.
  • labels (optional): free texts such as { "commit": "abc123" }, given back with the run.
  • A copy of the scenarios is taken at the start: changing the dataset afterwards does not change this run.
  • The monthly quotas: the simulation quota is checked at the start, and if it cannot cover every scenario, the run is refused (429 quota_exceeded) and nothing starts. The message quota is checked at each message: once it is reached, the remaining scenarios end failed.

Step 3: Read the results

curl -s "$BASE/v1/evaluation/runs/$RUN" -H "Authorization: Bearer $TOKEN" | jq

While the scenarios play, status is queued, then running. Run the command again every 5 to 10 seconds until it is completed, or cancelled if the run was cancelled. Here is the answer, with one of its two scenarios:

{
  "run_id": "run_2d2dad36…",
  "status": "completed",
  "progress": { "total": 2, "completed": 2, "handed_over": 0, "failed": 0, "cancelled": 0, "expired": 0, "queued": 0, "running": 0 },
  "simulations": [
    {
      "simulation_id": "sim_200dfc84…",
      "scenario_name": "change dates",
      "status": "completed",
      "final_state": "awaiting_user",
      "turns": 1,
      "handled_by": [ { "type": "use_case", "use_case_id": 5190, "name": "Post-Booking" } ],
      "metadata": { "expected": 5190 },
      "messages_unused": []
    }
  ]
}

A run ends completed even when some scenarios failed: judge each scenario. The table below says what to check.

Field of a scenarioThe scenario passes when…
statusit is completed. failed (no answer in 60 s, error of the bot, unknown click), handed_over (a handover was reached) or cancelled (the run was cancelled) need a look
handled_byit contains what you expect: the use case, or ai_assistant for Louis. Repeats are removed, but a use case reached once by a text and once by a click appears twice (its intent_record_id differs)
final_stateit matches the end you expect: ended, awaiting_user, handed_over or error
messages_unusedit is empty. Otherwise the scenario stopped before its last message

To read every turn of a scenario: GET /v1/evaluation/simulations/{simulation_id}.

Step 4: Fail your pipeline when a scenario fails

Save the script below as check_run.py. It waits for the run (15 minutes at most), prints one line per scenario, and exits with an error when one fails, which stops your pipeline. It needs Python 3 and the requests package: python3 -m pip install requests, in a virtual environment if your Python refuses global installs.

import os, sys, time
import requests

BASE = os.environ.get("VIASAY_API", "https://chatbot.viasay.io/api")
HEADERS = {"Authorization": "Bearer " + os.environ["VIASAY_TOKEN"]}
run_id = sys.argv[1]

for _ in range(90):  # 15 minutes at most
    run = requests.get(f"{BASE}/v1/evaluation/runs/{run_id}", headers=HEADERS, timeout=30).json()
    if run.get("status") in ("completed", "cancelled"):
        break
    time.sleep(10)
else:
    sys.exit(f"run {run_id} not finished after 15 minutes")

failed = 0
for sim in run["simulations"]:
    expected = sim["metadata"].get("expected")
    answered = [h.get("use_case_id") or h["type"] for h in sim["handled_by"]]
    ok = sim["status"] == "completed" and (expected is None or expected in answered)
    failed += not ok
    print("PASS" if ok else "FAIL", sim["scenario_name"], sim["status"], answered, sim["simulation_id"])

print(f"{len(run['simulations']) - failed} of {len(run['simulations'])} scenarios passed")
sys.exit(1 if failed else 0)

Run it with VIASAY_API=$BASE VIASAY_TOKEN=$TOKEN python3 check_run.py $RUN. On the run above, it prints:

PASS general question completed ['ai_assistant'] sim_f8cacf6e…
PASS change dates completed [5190] sim_200dfc84…
2 of 2 scenarios passed

Put the tests in your CI

Store the client ID and the secret in the secret variables of your CI (VIASAY_CLIENT_ID, VIASAY_CLIENT_SECRET), and the identifier of the dataset in VIASAY_DATASET_ID. The job below gets a token, starts the run and checks it.

set -euo pipefail
BASE='https://chatbot.viasay.io/api'
TOKEN=$(curl -s --fail-with-body -X POST "$BASE/oauth/token" \
  -u "${VIASAY_CLIENT_ID}:${VIASAY_CLIENT_SECRET}" \
  -d grant_type=client_credentials | jq -r .access_token)
RUN=$(curl -s --fail-with-body -X POST "$BASE/v1/evaluation/runs" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{ \"dataset_id\": \"$VIASAY_DATASET_ID\" }" | jq -r .run_id)
VIASAY_API=$BASE VIASAY_TOKEN=$TOKEN python3 check_run.py "$RUN"

Note: a new token revokes the previous one with the same scopes. Two jobs that run at the same time with the same application revoke each other's token: give each job its own application, or run them one after the other.

Next steps


Did this page help you?