Skip to content

Latest commit

 

History

History
81 lines (59 loc) · 4.2 KB

test-data.md

File metadata and controls

81 lines (59 loc) · 4.2 KB

Test data

Test data is kept apart from test logic. This guide says where each kind of value belongs and how tests stay independent of each other.

Where each value lives

Kind of value Location Example Changes per environment?
Environment settings config/environments/<env>.json baseUrl, readOnly yes
Application knowledge src/data/*.js routes, expected menu items, expected messages no
Credentials environment variables (.env, CI secrets) E2E_USERNAME, E2E_PASSWORD yes
Per-run values generated inside the test unique record names per run

Rule of thumb: if the value would differ between environments, it belongs in the environment file or in an environment variable. If it describes the application and is the same everywhere, it belongs in src/data.

Reading data in a test

Use the testData fixture, which returns the src/data module:

test(
  "Verify main menu lists all modules",
  { tag: [TAGS.INTEGRATION, TAGS.PROD] },
  async ({ dashboardPage, testData }) => {
    await expect(dashboardPage.menuItems).toHaveText(testData.dashboard.menuItems);
  }
);

Read environment settings through the env fixture, and build URLs with env.url(route):

await expect(page).toHaveURL(env.url(testData.routes.adminUsers));

Read credentials through the credentials fixture only in tests that need them. It fails with a clear message when they are missing, so a test never runs with an empty password.

Keeping tests independent

Independence means that any single test can run alone, in any order, and still pass.

  1. No shared mutable state. Do not store a value in a module variable and read it in another test.
  2. Each test sets up what it needs. Use a fixture, a beforeEach, or the saved session. Do not assume an earlier test created a record.
  3. Clean up what you create. If a test creates a record, it deletes that record in a finally block or an afterEach hook.
  4. Use unique names for created data. Include a run identifier so parallel runs do not collide, for example `qa-${Date.now()}`.
  5. Prefer existing data for reads. Searching for a known account is more stable than creating one for every run.
  6. Keep read-only tests read-only. The @prod rule in tagging depends on it.

The demo application

The sample suite runs against the public OrangeHRM demo. Its data is shared and reset periodically, so:

  • Assertions use stable facts: the account Admin exists, the menu has the same modules, the login page has the same fields.
  • Assertions avoid counts that change, such as the number of records. The search test checks that the first row contains the searched name.
  • Nothing in the suite creates or deletes records yet. When a test needs to, follow rule 3 above.

Adding a value

  • Static application value: add it to the matching object in src/data/orangehrm.js, or create a new module in src/data/ for a new area of the application.
  • Environment value: add it to every file in config/environments/. A key missing from one file fails validation.
  • Credential: add it to .env.example as an empty variable with a comment. Never add a value.

Accessibility baseline

src/data/a11y.js lists the accessibility rules that already fail on the login page. The accessibility test fails only on a violation whose rule is not in that list. A regression is caught, and the existing debt stays visible in the file. When a violation is fixed in the application, remove its entry, so it cannot come back unnoticed.

Factories (not yet)

The framework has no data factories. Add one when a test needs to create records that many tests share. A factory belongs in src/data/, returns unique values, and cleans up after itself.