Agent Wranglers

TST-33Recommended

Make every test able to fail

Every test must contain a check that fails when the behaviour breaks. Tests without assertions or with always-true checks are not tests.

How to do it

Watch each new test fail once before making it pass. Turn on lint rules that catch empty or assertion-free tests, such as vitest/expect-expect or jest/expect-expect. Where no such rule exists, mutation testing finds tests that never fail. Await every asynchronous assertion.

Examples

JavaScript example

// Bad: passes even when createUser is broken, because nothing is checked
test('creates a user', async () => { await createUser('ada@example.com'); });

// Good: fails when the user is not stored
test('creates a user', async () => {
  await createUser('ada@example.com');
  expect(await findUser('ada@example.com')).toMatchObject({ email: 'ada@example.com' });
});

Python example

# Bad: nothing is checked
def test_creates_a_user():
    create_user("ada@example.com")

# Good: fails when the user is not stored
def test_creates_and_stores_a_user():
    create_user("ada@example.com")
    assert find_user("ada@example.com").email == "ada@example.com"

How to check

The linter reports no test without an assertion. Breaking the code under a new test turns it red.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/test-quality/tests-can-fail">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.