Pre-conditions, post-conditions and invariants
Three small rules give a tester oracles for free: what must be true before an action, what must be true after it, and what must always be true.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a library loan desk. Before the desk lends a book, the book must be on the shelf. After the loan, the shelf has one book fewer and the borrower has one more. And all the time, the total number of books in the library stays the same. Three rules, and a clerk can spot a mistake at once.
Daily List is a to-do list app. A person can move a task from one list to another. Tom, the tester, wants rules that tell him the move worked, without a long script.
The first rule is the precondition: what must be true before the move. The second is the postcondition: what must be true after it. The third is an invariant, a fact that must hold at the agreed visible boundaries of each action. Tom writes all three for the move.
He then makes a , that notes a value before an action and compares it with the rule afterwards. The rules are the oracle: they say what a correct result looks like.
The picture has a limit. A library clerk sees the books. A tester must find a way to read each value, such as a count in the app or a row in a database.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 3 in this section
- precondition
- something that must be true first
- postcondition
- what is true when a use case has ended
- invariant
- always the same; never changing
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Writing what must be true before, after and at the declared invariant boundaries for an action, then reading values before and after to check.
- Before
- After
- Always
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| A book must be on the shelf before a loan | Precondition |
| The shelf has one fewer afterwards | Postcondition |
| The total number of books never changes | Invariant |
| The clerk counts before and after | Before-and-after check |
WHERE THIS PICTURE STOPS
- A clerk sees the books. A tester must find a way to read each value.
- A library has a few simple rules. Software may have many that interact.
- A clerk stops a loan on sight. The synthetic safe-refusal contract protects business state; a failed caller precondition alone does not specify refusal behavior.
HOOK
Before, after, invariant boundaries.
REMEMBER IT THIS WAY
A sequence: the real steps, in order.
For any move, write three lines: before it, what must be true; after it, what must be true; always, what must stay true.
| In the trick | What it stands for |
|---|---|
| Before | The precondition, true before the move |
| After | The postcondition, true after the move |
| Always | The invariant, true at the agreed visible action boundaries |
TELL A FRIEND
The three rules are a library desk: the book is on the shelf before, the shelf has one fewer after, and the total never changes.
OFTEN MIXED UP WITH How do you know the right answer? Test oracles. The oracle lesson asks how to judge a result. This lesson gives one source of oracles: what must be true before, after and at the declared invariant boundaries.
PRACTISE AND KEEP
Sources for this topic — 5 sources
- Market practice
Design by Contract (Eiffel course notes, Trinity College Dublin) ↗ — Trinity College Dublin (mathematics course notes) · A precondition is the caller's obligation, a postcondition is the supplier's obligation, and a class invariant is a property that must hold after creation and be kept by every routine (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Design by Contract (lecture slides, University of Bern) ↗ — University of Bern (Software Composition Group) · Lecture slides on preconditions, postconditions and invariants in design by contract (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Using Heuristic Test Oracles (Michael Kelly) ↗ — InformIT · A heuristic oracle is a way to judge a result that can be fallible, which is how a rule of what must be true is used in practice (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: article as listed on the publisher's site. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. Author attribution corrected from the author’s own bibliography https://www.michaeldkelly.com/articles, opened6 November2026. The InformIT article now redirects to its generic article index; article contents remain excerpt-only.
- Market practice
The Oracle Problem in Software Testing: A Survey (Barr, Harman, McMinn, Shahbaz, Yoo) ↗ — IEEE Transactions on Software Engineering, vol. 41, no. 5, 2015, pp. 507-525 · The oracle problem is the difficulty of deciding whether a result is correct (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: 2015, DOI 10.1109/TSE.2014.2372785. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Building bug-free O-O software: An Introduction to Design by Contract ↗ — Eiffel Software · Publisher material opened 6 November 2026; version and scope recorded in source registry and domain audit. Synthetic Daily List outcomes follow the stated teaching contract.
Undated publisher introduction opened 6 November 2026. Paraphrased for teaching; no licence claim.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.