Skip to content

Commit 45ff251

Browse files
niranjankagriclaude
andcommitted
Add framework design notes: decisions and alternatives
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8b4fa90 commit 45ff251

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

‎docs/framework-design.md‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Framework design
2+
3+
This page explains the decisions behind the framework and the alternatives that were not chosen.
4+
5+
## 1. One app, tested through its API and its UI
6+
7+
The Notes app has a web UI and a REST API on one backend. The test pyramid applies:
8+
9+
- Most rules are checked at the API level, which is fast and stable: validation, status codes,
10+
authorisation and the contract. There are 32 such tests.
11+
- The browser tests only check what a user sees and does. There are 9 of them.
12+
- A few hybrid tests check that the two sides agree. There are 5 of them.
13+
14+
## 2. Set-up through the API, never through the UI
15+
16+
UI tests need a logged-in user with some notes. Typing that into forms in every test would be slow,
17+
and it would repeat the login test hundreds of times. So instead:
18+
19+
- the `account` fixture registers a user with one API call;
20+
- the `notes_page` fixture puts that user's API token into `localStorage` before the page loads,
21+
which is the same place the app itself keeps it after a login. The browser starts out logged in;
22+
- notes for a test are created with `create_note` API calls.
23+
24+
The login form is still tested, once, in `tests/ui/test_login.py`.
25+
26+
## 3. Every test has its own user
27+
28+
Each test registers a new user with a unique e-mail (`uuid`) and deletes the user afterwards, in the
29+
fixture's teardown, which also runs after a failure. The results:
30+
31+
- No test depends on another test, or on the order in which tests run.
32+
- Tests run in parallel safely with `pytest-xdist -n 4`, and each browser job in CI runs in parallel
33+
with the others.
34+
- The public demo site is left clean.
35+
36+
A test that changes the password updates the mutable `Account` object, so the teardown can still log
37+
in. If the clean-up cannot log in, it gives a warning rather than an error: a test result should not
38+
depend on whether the clean-up worked.
39+
40+
## 4. The API client returns responses; it does not assert
41+
42+
`NotesApiClient` methods return `requests.Response`. Tests decide what is right. This way the same
43+
client serves happy-path tests (200/201) and negative tests (400, 401, 404, 409). If a client method
44+
asserted "200", the negative tests would need a second client.
45+
46+
The client keeps a `requests.Session`, which reuses connections, and the login token, which it sends
47+
as `x-auth-token`.
48+
49+
## 5. Contracts with pydantic
50+
51+
`models.py` describes each response body. `ApiResponse[Note].model_validate(json)` checks:
52+
53+
- every field is present and has the right type;
54+
- `category` is one of `Home`, `Work`, `Personal` (a `Literal`);
55+
- there are no unknown fields (`extra="forbid"`). A field that the backend adds or renames shows up
56+
as a failing test straight away, not as a silent change.
57+
58+
The generic `ApiResponse[T]` models the envelope (`success`, `status`, `message`, `data`) once, for
59+
every endpoint.
60+
61+
## 6. Page Objects without assertions; `expect` in tests
62+
63+
Page Objects (`LoginPage`, `NotesPage`) hold locators and actions. Locators use the app's
64+
`data-testid` attributes (`get_by_test_id`), which do not change when the text or the layout does.
65+
66+
Assertions are in the tests, with Playwright's `expect(locator).to_have_text(...)`. This waits until
67+
the condition is true or the timeout ends, so there are no `sleep` calls and no flaky timing checks.
68+
69+
`NotesPage.wait_until_loaded()` waits for whichever appears first: the progress text (when notes
70+
exist) or the "no notes" message (when there are none). It uses `locator.or_()` and
71+
`filter(visible=True)`, because the progress element is in the DOM but hidden on an empty list.
72+
73+
## 7. Configuration from environment variables
74+
75+
`framework/config.py` reads `NOTES_APP_URL`, `NOTES_API_URL` and two timeouts, with defaults for the
76+
public demo. The same code then runs against a local copy, a staging copy or the demo, and CI needs no
77+
config files. `Settings` is a frozen dataclass and is created once per run (a session fixture).
78+
79+
## 8. Markers and selection
80+
81+
`api`, `ui`, `e2e`, `smoke` and `demo` are declared in `pytest.ini`, and `--strict-markers` fails a
82+
run that uses a misspelt marker. CI uses the markers to split the jobs: `-m api` runs without a
83+
browser, and `-m "ui or e2e"` runs once per browser. The demo tests carry `demo`, which `addopts`
84+
leaves out (`-m "not demo"`), so they only run when asked for.
85+
86+
## 9. Reports and failure evidence
87+
88+
- **pytest-html** writes one self-contained file, which is easy to keep as a CI artifact or to send to
89+
someone. A `pytest_runtest_makereport` hook adds a full-page screenshot to every failed UI test.
90+
`pytest_configure` adds the app and API URLs to the environment table.
91+
- **Playwright** keeps a screenshot, a video and a trace of every failed UI test
92+
(`--screenshot=only-on-failure --video=retain-on-failure --tracing=retain-on-failure`). Passing
93+
tests keep nothing, so the artifacts stay small. The trace is usually the quickest way to see
94+
why a test failed.
95+
- **The GitHub run summary** shows a pass/fail table per job, built from the JUnit XML by
96+
`.github/scripts/summary.py`, so nobody has to download an artifact to see what failed.
97+
98+
## 10. Failing on purpose: the report demo
99+
100+
`tests/demo` has one test for each kind of non-passing result, so the report can be shown without
101+
breaking real tests:
102+
103+
- **failed**: a UI assertion with a screenshot, and an API assertion that includes the API's message;
104+
- **skipped**: `pytest.skip` with a reason;
105+
- **xfailed**: `xfail(strict=True)`. A known bug is recorded without failing the run. Once the bug is
106+
fixed, the test passes, and `strict` turns that into a failure, as a reminder to remove the mark.
107+
108+
## 11. Public demo site: blocking ads
109+
110+
The demo site loads ads and trackers. They slow down page loads, and an ad can cover a button. The
111+
`app_page` fixture blocks those hosts with `context.route(...)`, before the first page is opened.
112+
113+
## 12. CI
114+
115+
- **lint** runs first, and the test jobs need it to pass. A formatting slip costs a few seconds,
116+
not a full browser run.
117+
- **The browser matrix** uses `fail-fast: false`, so a Firefox-only problem does not hide the
118+
Chromium and WebKit results.
119+
- **Each browser job** installs only its own browser (`playwright install --with-deps <browser>`).
120+
- **A nightly cron run** notices when the public site changes, even when the code does not.
121+
- **`workflow_dispatch`** can run the demo markers, to show failures in CI.
122+
123+
## Not chosen
124+
125+
| Option | Why not |
126+
|---|---|
127+
| Logging in through the form in every UI test | slow, and repeats one test hundreds of times |
128+
| Shared test users | tests would depend on each other and could not run in parallel |
129+
| `time.sleep` / custom wait loops | `expect` and auto-waiting locators already wait for the right condition |
130+
| CSS or XPath locators | `data-testid` is stable and made for tests |
131+
| Allure | pytest-html gives one self-contained file with no extra tool to install; the traces already give step-by-step detail |
132+
| Assertions inside the client or the page objects | negative tests would need other helpers, and a failure would point at framework code instead of the test |

0 commit comments

Comments
 (0)