First Steps

Write Your First Test

Write Your First Test

A test in Curlex is a short piece of JavaScript that runs automatically after a response arrives and checks that the response is what you expected. If the check passes, it shows as green. If it fails, it shows as red with a clear message explaining what went wrong.

Tests run every time you click Send — and also when you use the Collection Runner to run an entire collection automatically.

This guide walks through writing your first test in about five minutes.


Step 1 — Open a Request with a Predictable Response

We will use a free public API so you can follow along exactly. Open a new request tab and configure it:

  • Method: GET
  • URL: https://jsonplaceholder.typicode.com/posts/1

Click Send to make sure it works. You should see a 200 OK response with a JSON body like this:

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur..."
}

Good — now you know what the response looks like. Let us write a test that verifies it.


Step 2 — Open the Scripts Tab

Click the Scripts tab in the request panel (fifth tab from the left). You will see two editors:

  • Pre-Request Script (top) — runs before the request is sent
  • Test Script (bottom) — runs after the response arrives

We will write in the Test Script area.


Step 3 — Write Your First Assertion

Click in the Test Script editor and type:

fc.test("Returns 200 OK", function () {
    fc.response.to.have.status(200);
});

Now click Send again. After the response arrives, click the Test Results tab in the response panel. You will see:

✅  Returns 200 OK

Congratulations — your first test passed.

What just happened

  • fc.test("name", function() { ... }) defines a test with a name and a function containing your assertions.
  • Inside the function, fc.response.to.have.status(200) is the assertion — it checks that the response status code is 200.
  • If the assertion passes, the test is green. If it fails, it is red with a message.

Step 4 — Test a Value in the Response Body

Add a second test below the first one:

fc.test("Returns 200 OK", function () {
    fc.response.to.have.status(200);
});

fc.test("Response belongs to user 1", function () {
    var body = fc.response.json();
    fc.expect(body.userId).to.equal(1);
});

Click Send again. Both tests should pass.

What fc.response.json() does

fc.response.json() parses the response body as JSON and gives you a JavaScript object you can work with normally. body.userId reads the userId field, and fc.expect(...).to.equal(1) checks it equals 1.


Step 5 — Intentionally Fail a Test

To understand what a failure looks like, add a test you know will fail:

fc.test("Title is exactly five words", function () {
    var body = fc.response.json();
    var wordCount = body.title.split(" ").length;
    fc.expect(wordCount).to.equal(5);
});

Click Send. The Test Results tab will show:

✅  Returns 200 OK
✅  Response belongs to user 1
❌  Title is exactly five words
    expected 10 to equal 5

The failure message tells you exactly what was expected and what actually happened. This is how you will diagnose failures when testing real APIs — the error message points directly at the mismatch.

Remove this failing test before moving on.


Step 6 — Check a Response Header

fc.test("Response is JSON", function () {
    fc.response.to.have.header("content-type");
});

This passes if the response includes a Content-Type header (regardless of its value). To also check the value:

fc.test("Content type includes application/json", function () {
    var contentType = fc.response.headers.get("content-type");
    fc.expect(contentType).to.include("application/json");
});

Step 7 — Check Response Speed

fc.test("Responds in under 2 seconds", function () {
    fc.expect(fc.response.responseTime).to.be.below(2000);
});

fc.response.responseTime is the total round-trip time in milliseconds. This test passes if the response came back in under 2 seconds.


Your Complete Test Script

Here is everything together:

fc.test("Returns 200 OK", function () {
    fc.response.to.have.status(200);
});

fc.test("Response belongs to user 1", function () {
    var body = fc.response.json();
    fc.expect(body.userId).to.equal(1);
});

fc.test("Post has an id", function () {
    var body = fc.response.json();
    fc.expect(body.id).to.exist;
});

fc.test("Post has a title", function () {
    var body = fc.response.json();
    fc.expect(body.title).to.be.a("string");
    fc.expect(body.title.length).to.be.above(0);
});

fc.test("Content type is JSON", function () {
    var contentType = fc.response.headers.get("content-type");
    fc.expect(contentType).to.include("application/json");
});

fc.test("Responds quickly", function () {
    fc.expect(fc.response.responseTime).to.be.below(2000);
});

Using Tests in the Collection Runner

Once you have tests on your requests, you can run an entire collection and see the aggregate pass/fail report. Open the Collection Runner (Runner icon in the sidebar rail), choose your collection, and click Run. Every test assertion from every request appears in the results.

This is how you run a full regression suite after a deployment in a single click — and why writing tests as you build requests pays off later.


Next Steps