QA Interview Hub · Knowledge base · Quizzes · Українська

Patterns › SOLID, DRY, KISS in tests, and anti-patterns

Test code as production code: review, linters and metrics

Test code is maintained like production code: review asks whether a test can fail, a linter catches mechanical errors, and metrics show a trend. Metrics: share of flaky tests, run time, switched-off tests, cost of change. They are not a plan, because then the measure is «met» by deleting tests.

Patterns · Test code quality

A test failed on Friday, and the file has 400 lines, a 5-second pause and a commented-out assertion. What does such a «temporary» fix cost?

Test code lives for years, it is read more often than written, and when it fails, the pipeline of the whole team stops. Yet test review often boils down to «it works, merge», and the linter is switched off for test folders. The result is familiar: the suite grows, flaky tests multiply, and trust in a red run disappears. This lesson is about carrying over to tests the same practices as for production code, and about what to measure.

Mechanism: why a test costs more than its size. A test has no value of its own unless the team trusts it. Every unstable test takes time from everyone who sees a red run: they have to find out whether it is a defect or noise. So the cost of a bad test is multiplied by the number of people and runs, not by the number of lines. Three practices follow: check a test before the merge (review), catch typical mistakes automatically (linter, types, config settings) and watch the state of the suite with numbers.

Three gates for a test

A test changethe author sees a green run1. Linter and tsc (machine)await, pause, force, .only2. Review (person)can this test fail at all3. CI and metricsforbidOnly, flaky share, run timewhat slips through costs the whole team
Figure 1. Each gate catches its own kind: the machine removes the mechanical, meaning stays with a person.

Reviewing a test. The questions differ from reviewing production code, because the first one is: can this test fail.

  • Does the assertion check behaviour, not only that a step was executed? A test without an assertion is always green. A useful trick: temporarily break the code or the expected value and make sure the test turns red.
  • Is the test isolated: its own data, no dependence on order, cleanup after itself?
  • Are there no fixed pauses, force, if branches and loops inside the test that hide what is being checked?
  • Does the title read like a requirement («rejects an expired card»), not like an implementation?
  • Is repeated code moved to a fixture or Page Object, but without premature abstraction (see the lesson on DRY and DAMP)?

Linter and types. What a machine can check should not be left to the reviewer. For Playwright there is the eslint-plugin-playwright plugin: its recommended set includes the rules missing-playwright-await (a forgotten await), no-wait-for-timeout, no-force-option, no-focused-test and no-skipped-test, expect-expect (a test without an assertion), prefer-web-first-assertions, no-conditional-in-test and no-networkidle.

import { defineConfig } from 'eslint/config';
import playwright from 'eslint-plugin-playwright';

export default defineConfig([
  {
    files: ['tests/**'],
    extends: [playwright.configs['flat/recommended']],
  },
]);

The Playwright documentation also advises the @typescript-eslint/no-floating-promises rule and a tsc --noEmit check in CI. In the config it is worth blocking a forgotten test.only from being merged: the forbidOnly option, turned on only in CI. All this works as a gate: the mistake does not reach the review.

Example: before → after. This test passed an «it works, merge» review and is always green:

test('checkout', async ({ page }) => {
  await page.goto('/cart');
  await page.getByRole('button', { name: 'Pay' })
    .click({ force: true });
  await page.waitForTimeout(5000);
  // await expect(page.getByText('Paid')).toBeVisible();
});

The assertion is commented out, the pause masks slowness, and force hides that the button is covered. A linter would flag all three (expect-expect, no-wait-for-timeout, no-force-option). After review the test turns red when the payment did not happen:

test('pays for the cart', async ({ page }) => {
  await page.goto('/cart');
  await page.getByRole('button', { name: 'Pay' }).click();
  await expect(page.getByText('Paid')).toBeVisible();
});

Maintainability metrics. The numbers are for seeing a trend, not for setting targets.

  • Share of flaky tests: Playwright marks in the report a test that failed and passed on a retry; the count of such tests per week shows whether instability grows. The --fail-on-flaky-tests flag turns it into a red run where the team is ready to deal with it.
  • Run time (median and p95) and the wait for a result in a PR: a slow suite stops being run.
  • How many tests are switched off with skip or fixme and for how long: a test switched off without a deadline is hidden debt.
  • Cost of change: how many tests have to be fixed after one interface change. A large number signals that locators and steps are not moved into one layer.
  • Time from a red run to the fix: it shows whether the tests have owners.

Limits. A linter does not know whether a test checks the right thing: expect-expect is satisfied by any assertion. Metrics are easy to turn into a target, and then they are «improved» by deleting tests or adding extra retries, which Goodhart’s law describes in general: a measure that becomes a target stops being a measure. Strict rules on an old suite produce thousands of remarks, so they are first turned on for new code or as warnings, and raised to errors gradually. And test review must not be heavier than code review: if it takes longer, the team will bypass it.

Common mistakes.

  • The linter is switched off for the tests folder «so it does not get in the way»: half a year later it holds every violation the linter was meant to catch.
  • Tests are reviewed only for style, not for whether they can fail.
  • «Number of tests» or «coverage» is used as a target: tests without assertions appear.
  • A flaky test is covered with retries and forgotten: the run is green while instability piles up.

Check yourself

The linter is on, the expect-expect rule gives no remarks, and yet the test never fails. How is that possible and what do you do?

Answer

The rule checks only that an assertion exists, not what it means: a test can compare a value with itself or check that an element exists when its text should be checked. Only review catches this: temporarily break the behaviour and see whether the test turns red. A linter removes mechanical mistakes, and the meaningful ones stay with a person.

The team wants the share of flaky tests below 2%. What can go wrong if this becomes a plan?

Answer

The number can be «met» without stability: delete the unstable tests, add more retries or stop marking them as flaky. So the number is used as a trend and a reason to look into specific tests, and the target is stability and reaction time.

In an interview. Junior: A test is code, so it is reviewed, linted and maintained too: a test without an assertion or with a fixed pause does not pass review. Middle/Senior: Machine rules (eslint-plugin-playwright, no-floating-promises, forbidOnly in CI) catch mechanical mistakes, and review is left for meaning: can the test fail. I measure the state of the suite as a trend: share of flaky tests, run time, switched-off tests, cost of change; I do not make these numbers a plan, because then they are «met» by deleting tests.

More articles in this chapter