<!-- https://qasignoff.com/docs/core-features/tests-scripts/ -->
# Tests & scripts

* **Platform Availability:** Windows App · VS Code Extension
* **Overview:** Check responses with or without code, and run scripts before or after a request. Scripts from Postman, Bruno, Thunder Client, Insomnia and Hoppscotch are supported too.

## Add checks without code

Open **Tests** and click **Add assert** to check the status, response time, headers, body or a JSONPath value. You can also click **Assert** in a response to [choose from suggested checks](/docs/core-features/requests/).

![Tests tab with no-code checks and a test script](/docs/assets/screenshots/app/request-tests.webp)

## Write a script

- **Pre-request Script** runs before the request.
- **Tests** runs after the response.
- For a script shared by a whole collection, open the collection and choose **Scripts** or **Tests**. For a folder, right-click it and choose **Open Scripts**.

![Pre-request Script editor](/docs/assets/screenshots/app/request-pre-request.webp)

Use `qa.*` for new scripts:

```javascript
// Pre-request Script
qa.variables.set("ts", String(Date.now()));
```

```javascript
// Tests
qa.test("status is 200", () => {
  qa.expect(qa.response.code).to.equal(200);
});

qa.test("the response includes a URL", () => {
  const body = qa.response.json();
  qa.expect(body.url).to.be.a("string");
});

// Save a value for a later request
qa.variables.set("gotUrl", qa.response.json().url);
```

Type `{{` in a script to choose a variable. Script errors point to the line in your own code.

## Use scripts from other tools

| Tool | Supported scripts |
|------|-------------------|
| Postman | `pm.*`, plus older scripts that use `responseBody`, `tests[...]` and `postman.*` |
| Bruno | `bru.*`, `req` and `res` |
| Thunder Client | `tc.*` |
| Insomnia | `insomnia.*` |
| Hoppscotch | `pw.*` and `hopp.*` |

```javascript
pm.test("status is 200", function () {
  pm.response.to.have.status(200);
});
```

`pm.expect`, `insomnia.expect`, and `expect` in Bruno and Thunder Client use Chai, so its checks work the same way.

### Load libraries

`require()` works with these bundled libraries:

- **Checks and validation:** Chai, tv4, Ajv
- **Data and dates:** lodash, Moment, uuid, csv-parse, papaparse
- **XML and security:** xml2js, fast-xml-parser, crypto-js, jsonwebtoken
- **Requests and built-ins:** axios, node-fetch, crypto, url, path, querystring, util, events, assert and buffer

Other npm packages aren't available. If a module can't run in the script sandbox, such as `fs`, the error suggests an alternative.

### What isn't supported

These calls stop with a clear error: `pm.execution.runRequest`, `bru.runRequest`, `tc.runRequest`, `tc.readFile` and `tc.exec`. `req.onFail` and `bru.getProcessEnv` only log a warning.

## Wait and run more requests

- `qa.sleep`, `tc.delay`, `bru.sleep`, `setTimeout` and `setInterval` really do wait. Each script can spend up to 60 seconds on timers and extra requests.
- `qa.sendRequest` sends another request from a script. Make sure it can't keep calling itself.
- Scripts can also use WebSocket, SSE and gRPC through `qa.ws`, `qa.sse` and `qa.grpc`.
- What you can do with `qa.db`, `qa.load`, `qa.files` and `qa.ui` depends on where the script runs.

## How long variables last

In a collection run, a value set in one request is available to later requests. This works with `qa.variables.set`, `pm.variables.set` and `bru.setVar`. Numbers and objects keep their type.

| Collection from | What happens to these values |
|-----------------|------------------------------|
| SignOff Studio or Postman | Kept for the run, but cleared between manual sends |
| Bruno | Kept for the run and between manual sends |

In a run with a data file, a value set with `pm.variables.set` in a SignOff Studio or Postman collection takes priority over a column with the same name. In Bruno collections, the data file takes priority. Variables declared on a request last only for that request.

Values of variables marked as secret appear as `[SECRET]` in script logs.

## Next

- [Environments & variables](/docs/core-features/environments/)
- [Collection Runner](/docs/core-features/runner/)
- [Import & export](/docs/core-features/import-export/)
