API tests
Your coding agent writes an API test: requests in order, values passed between them, assertions on each response, run on a sandbox and added to your suite.
An API test sends a few requests to your app's API in order, passes values from one response into the next request, and checks what comes back. It needs no browser. Your agent writes it as a short JSON file and runs it on a sandbox, a test copy of your app.
In the CLI, an API test is an API-only workflow with one API sequence. The commands below use those names.
Write and run an API test
-
Find the requests to call. Your agent searches the API catalog, the list of requests Cofactor knows about for an application.
bashcofactor api-catalog endpoints list --search projects --application <application id> --json
Each result has an
id, amethodand anexampleUrl. If the request you need is missing, add it in one of three ways:-
Run a browser test on the application. Calls the browser makes during the run are added to the catalog.
-
Import an OpenAPI description from a URL. Save this as
source.json, then run the command below it.json{ "url": "https://api.example.com/openapi.json", "propertyId": "<application id>", "formatId": "openapi", "formatLabel": "OpenAPI 3.1.0", "baseUrl": "https://api.example.com" }bashcofactor api-catalog sources create --from-file source.json
-
Add one request. Save
{"method": "GET", "url": "https://api.example.com/projects", "propertyId": "<application id>", "name": "List projects"}asendpoint.json, then runcofactor api-catalog endpoints create --from-file endpoint.json.
cofactor application listprints each application's id. For a long catalog, add--pageand--page-sizeto the search. -
-
Write the sequence. Save this as
seq.json. Each step names a catalog request byapiRequestId. A later step reads an earlier response with{{stepId.response.path}}, andvalidationsassert on the response.json{ "description": "Create a project, read it back, delete it", "steps": [ { "stepId": "create", "apiRequestId": "<id of POST /projects>", "credentialId": null, "credentialTemplateName": "admin", "urlTemplate": "$var.ENV.api_url/projects", "bodyTemplate": "{\"name\": \"Project $RANDOM_STRING(6)\"}", "expectedStatusCodes": [201], "validations": [{ "left": "create.response.id", "operator": "is_set" }] }, { "stepId": "read", "apiRequestId": "<id of GET /projects/:id>", "credentialId": null, "credentialTemplateName": "admin", "urlTemplate": "$var.ENV.api_url/projects/{{create.response.id}}", "expectedStatusCodes": [200], "validations": [{ "left": "read.response.status", "operator": "equals", "right": "active" }] }, { "stepId": "remove", "apiRequestId": "<id of DELETE /projects/:id>", "credentialId": null, "credentialTemplateName": "admin", "urlTemplate": "$var.ENV.api_url/projects/{{create.response.id}}", "expectedStatusCodes": [204] } ] }Three details keep the test reliable:
- Start each URL with
$var.ENV.api_url. The test then calls whichever environment it runs on. A step withouturlTemplatecalls the address stored in the catalog. credentialTemplateNameis the persona that makes the call. Use a persona name fromcofactor sandbox show.- Set
expectedStatusCodeson every step. A step without it passes on any 2xx response.
- Start each URL with
-
Save the test.
bashcofactor workflow create --api-only --name "Projects API: create, read, delete" --sequence-from-file seq.json
The command prints the workflow id. Use it as
<workflow id>below. If the file has a mistake, the command lists each one aspath [CODE] :: message. Fix them and run it again. To change a saved test, runcofactor workflow api-sequence update <workflow id> --from-file seq.json.An API test with three steps. Step 2 is open and lists two assertions: one checks a field equals a value, the other that a field is set. -
Run it on a sandbox. Claim a sandbox, then run the test on it.
bashcofactor sandbox claim --application "Acme App" --wait cofactor workflow run <workflow id> --sandbox <sandbox id> --stream
The command prints
Run finished: successorRun finished: failed. A failed run prints an error that names the first failed step, such asAPI sequence failed at step read, and the command exits with code 1. To read what a step sent and got back, runcofactor workflow runs get <workflow id> <run id> --jsonto find the run step id insteps[].id. Then add--step <run step id> --api-call <stepId>, where<stepId>is the step's name inseq.json, such asread. -
Keep it. Add the passing test to Regression, your default suite. This changes what your default suite runs, so confirm it first. Then destroy the sandbox.
bashcofactor workflow add-to-regression <workflow id> cofactor sandbox destroy <sandbox id>
Check it worked
cofactor workflow runs list <workflow id> --side api cofactor workflow list --in-regression true
The latest run reads success, and the test is in the Regression list. In the app, open E2E tests, choose the test and open its Runs tab to see each run.
Select a run to see each call with its method, address, status and time.
Go deeper
- API sequence reference lists every field, assertion and variable.
- Suites and schedules runs your tests together, in CI and every night.
- Macros and variables covers
$RANDOM_STRING(),$EMAIL()and the rest. - Seed data and logins covers the personas a test calls as.
- Run
cofactor workflow guide api-sequencesfor the authoring reference, orcofactor api-catalog --helpfor every catalog command.