Test a resource from create to delete: a CRUD lifecycle folder

Most API bugs that reach production aren't in one endpoint. They're between endpoints: the list doesn't show the record you just created, the update changes a field the read doesn't return, the delete returns 204 but the record is still there.

A lifecycle folder tests the whole life of one record, in order, with fresh data every run.

The folder

Output
๐Ÿ“‚ Projects lifecycle
  POST   Create project
  GET    Read project
  GET    List projects (contains new one)
  PATCH  Rename project
  GET    Read project (after rename)
  DELETE Delete project
  GET    Read project (after delete)

The runner sends requests in tree order, so name and order them as you want them to run. Values a script sets are passed to the requests after it in the same run.

1. Create

HTTP
POST {{baseUrl}}/projects
Content-Type: application/json

{"name": "lifecycle {{$randomUUID}}", "visibility": "private"}
JavaScript
pm.test('created', () => pm.response.to.have.status(201));
const project = pm.response.json();
pm.environment.set('projectId', project.id);
pm.environment.set('projectName', project.name);

pm.test('has an id and timestamps', () => {
  pm.expect(project.id).to.be.a('string');
  pm.expect(project.createdAt).to.be.a('string');
});

A random name means two runs at the same time don't collide, and you can find leftovers later by the lifecycle prefix.

If the API returns a Location header, check it too: pm.expect(pm.response.headers.get('Location')).to.include(project.id).

2. Read

JavaScript
pm.test('read returns what create returned', () => {
  const p = pm.response.json();
  pm.expect(p.id).to.equal(pm.environment.get('projectId'));
  pm.expect(p.name).to.equal(pm.environment.get('projectName'));
  pm.expect(p.visibility).to.equal('private');
});

3. List

Lists often come from a different query, a cache or a search index, which is exactly why they drift:

HTTP
GET {{baseUrl}}/projects?limit=100&sort=-createdAt
JavaScript
pm.test('new project is in the list', () => {
  const ids = pm.response.json().data.map((p) => p.id);
  pm.expect(ids).to.include(pm.environment.get('projectId'));
});

If the list is eventually consistent, say so in the test name, and give it a delay in the runner (Delay between requests, or --delay in the CLI) rather than letting it fail at random.

4. Update, then read again

HTTP
PATCH {{baseUrl}}/projects/{{projectId}}
Content-Type: application/json

{"name": "{{projectName}} renamed"}
JavaScript
pm.test('updated', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));

The read after it checks two things: the field changed, and the fields you didn't send didn't:

JavaScript
const p = pm.response.json();
pm.test('name changed', () => pm.expect(p.name).to.equal(`${pm.environment.get('projectName')} renamed`));
pm.test('visibility untouched by PATCH', () => pm.expect(p.visibility).to.equal('private'));

The second check catches a PATCH that behaves like a PUT and resets missing fields to defaults.

5. Delete, then read again

JavaScript
// Delete project
pm.test('deleted', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));
JavaScript
// Read project (after delete)
pm.test('gone', () => pm.expect(pm.response.code).to.be.oneOf([404, 410]));

Add a second delete if you want to pin down whether deleting twice is 404 or 204. Either is fine; changing between releases is not.

Run it

Terminal
requestarium run-collection "Projects API" --folder "Projects lifecycle" --env Staging --bail

--bail stops at the first failure, which is what you want here: if create fails, the other six requests have nothing to work with.

In a test suite, mark Create project as a Setup step and Delete project as Clean up. Clean-up steps run even after a failure, so a failed run doesn't leave test projects behind. See setup and clean-up steps.