Skip to content

Testing your Application: explain why interdependent tests are bad #683

@bajtos

Description

@bajtos

In strongloop-archive/loopback4-example-getting-started#2, the proposed acceptance tests are relying on the first test to fill the database with data that's used by subsequent tests:

  it('creates a todo', async () => {
    item = (await client
      .post('/todo')
      .send(payload)
      .expect(
        Object.assign({}, payload, {
          id: 1
        })
      )).body;
  });

  //...

  it('successfully deletes todos', async () => {
    await client.del(`/todo/${item.id}`).send();
    await client
      .get(`/todo/${item.id}`)
      .send()
      .expect(404);
  });

(see strongloop-archive/loopback4-example-getting-started#2 (comment))

This is an anti-pattern to avoid:

  • if the first test fails, all subsequent tests fail too (because data was not filled in), but with an unhelpful error message is confusing.
  • it is not possible to run individual tests on their own, e.g. via it.only() or mocha -g "test name".

We should extend Data Handling in Testing your Application to mention this pattern and explain why it's a bad thing to do.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions