Basic

Testing state that a page depends on

A page that shows saved state depends on data the test has to set up first. The rule is: the test creates its own state, asserts on it, and cleans it. A test that reads whatever state exists is a test that changes with the site.

What you will see

Both runs pass. A second run that fails means the test left state behind.

About 1 min of reading

Try It

Test one piece of saved state

1

Create the state

curl -s -i -c /tmp/jar -d 'email=fe-test@example.com&password=Testpass123' http://localhost:3000/login | head -4

Before you run it, say out loud what it will print. Then open the answer.

What it prints
A session cookie in the jar for an account the test made.
2

Assert the created item shows

curl -s -b /tmp/jar http://localhost:3000/favorites | grep -c 'the-item-the-test-added'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Above zero for the item this test added. Do not assert a total count.
3

Clean it

curl -s -b /tmp/jar -o /dev/null -w '%{http_code}\n' "http://localhost:3000/favorites/remove/the-item-the-test-added"

Before you run it, say out loud what it will print. Then open the answer.

What it prints
200. The item is gone and the next run starts clean.
4

Run the test twice

./fe-state.sh; ./fe-state.sh

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Both runs pass. A second run that fails means the test left state behind.
Do not assert a list length. Another user's favorites change the length and the test fails on unrelated data.

Data Flow

Test to its own state
1 Create the state the test needs
↓
2 Load the page
↓
3 Assert the created item is shown
↓
4 Remove the created state
Why this works
What clean-up is for

Repeatability. A test that leaves state behind fails the next run, and a suite that fails on its own leftovers is not a suite.

Where state is expensive

If setting up state costs a slow job, split the assertion into a state test and a render test, and assert the render on a fixture.

Examples

Create, assert, clean

Add one item, assert the page shows one item, remove it. The three steps leave the site as it started, so the test can run twice.

Never assert an absolute total

Assert the item you added is present, not that the list has a fixed length. Another user's data changes the total and the test fails on someone else's work.

Own the identity

Use a test account made by the test. A shared account makes two runs of the suite interfere with each other.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Testing a form without a browser
Previous Lesson
Testing a form without a browser
Next Lesson
Reporting a frontend test so a person can act
Reporting a frontend test so a person can act →