From business rule to test case
Turn a rule into “Given, when, then”, choose data that proves it, cover the edge cases.
Lesson 712 min
By the end of this lesson
- Turn a business rule into a given, when, then test case
- Choose the edge cases that put a rule to the test
- Handle an uncovered rule and interpret a surviving mutant
In this lesson4
A business rule rarely arrives as a test. It arrives as a sentence: "a market order has priority over a limit order". Your job is to translate it into a starting state, an action and an expected result, checkable by a machine.
Test case
A business rule written as "Given, when, then": a starting state, an action, an expected result.
From sentence to test, in three questions
Take the rule from the BVMAC General Rules 2025, art. 159: a market order has priority over a limit order.
- Given: which state makes the rule observable? You need a limit buy that arrived before a market buy, otherwise time priority alone would explain the result.
- When: which action triggers the rule? An auction in which both buy orders are filled, but not both in full.
- Then: which result, precise and quantified? The first execution goes to the market order.
it('fills the market order before the earlier limit order', () => {
const r = fixing([
{ id: 'a1', sens: 'achat', quantite: 30, limite: 10050, rang: 1 },
{ id: 'a2', sens: 'achat', quantite: 20, limite: null, rang: 2 },
{ id: 'v1', sens: 'vente', quantite: 25, limite: 9950, rang: 3 },
{ id: 'v2', sens: 'vente', quantite: 40, limite: 10050, rang: 4 },
], 10000)
expect(r.executions[0]).toEqual({ achat: 'a2', vente: 'v1', prix: 10050, quantite: 20 })
})
The choice of data makes the test. With a2 arriving first, this test would pass even if the engine ignored the rule: it would prove nothing.
The expected price, 10,050, can be predicted with the tie-break rules of The auction in code: work out the curve before writing the "then", otherwise you copy the engine's output instead of checking it.
The scene follows the life of this test. A first draft, where the market order arrives first, passes against the engine; it also passes against a mutant that serves buyers by rank alone, without the rule. The second draft, the code above, passes against the engine and fails against the mutant: only it proves the rule.
“A market order has priority over a limit order.”
rule received
A business sentence
An engine's edge cases
Bugs live at the edges. For each rule, look for the values that put it to the test:
- quantities: zero, negative, decimal, exactly equal to what rests opposite;
- prices: a price tie between two orders, a limit exactly equal to the best opposite price, a non-integer price;
- market orders: with no counterparty, alone on both sides, facing an empty book;
- auction: no cross, a volume tie between several prices, imbalances of opposite signs;
- life cycle: cancelling an order already filled, cancelling twice, reusing an identifier.
Pick a case, read the rule, then run the test against the engine.
- Given
- an empty book
- when
- a buy of 0 shares at 10,000 arrives
- then
- the engine rejects the order with an exception
const c = new Carnet()
expect(() =>
c.passer({ id: 'a1', sens: 'achat', quantite: 0, limite: 10000 }),
).toThrow()From memory: why does the market-priority test make the limit order arrive first?
A rule the engine does not know yet
The rules allow "all or nothing" orders, executed in full or not at all (BVMAC General Rules 2025, art. 167). The teaching engine's Ordre type has no field to express this. Write the case anyway: it becomes the specification of the feature.
it.todo('an all-or-nothing order for 50 against 30 available produces no execution')
Challenge: your first test case
Write, as Given / when / then and then in TypeScript, the following rule: amending an order loses time priority if the limit changes or the quantity increases, but keeps it if the quantity decreases (Euronext Trading Manual, 2021, section 2). The engine has no amend method: first decide how to represent it (cancel and resubmit? a new method?), then write both tests.
One possible answer, with its tests, is in Amending and cancelling an order.
Going further
Who tests the tests?
The scene showed it: a green test says nothing if it would stay green without the rule. Mutation testing makes this question systematic.
Mutation testing
A technique that deliberately alters the code (a mutant) to check that at least one test fails.
A mutation tool alters the source code itself: a >= becomes >, a condition disappears. Without a tool, you can already simulate a targeted mutant by transforming the inputs. Here, replacing each market order with a limit order at the most aggressive price in the book removes exactly the "market first" rule, without changing the equilibrium price:
// Mutant: the market order becomes a limit order, it loses its priority
const sansRegleMarche = (ordres: Ordre[]) =>
fixing(
ordres.map((o) => (o.limite === null ? { ...o, limite: o.sens === 'achat' ? 10050 : 9950 } : o)),
10000,
)
const ordres: Ordre[] = [
{ id: 'a1', sens: 'achat', quantite: 30, limite: 10050, rang: 1 },
{ id: 'a2', sens: 'achat', quantite: 20, limite: null, rang: 2 },
{ id: 'v1', sens: 'vente', quantite: 25, limite: 9950, rang: 3 },
{ id: 'v2', sens: 'vente', quantite: 40, limite: 10050, rang: 4 },
]
fixing(ordres, 10000).executions[0]
// { achat: 'a2', vente: 'v1', prix: 10050, quantite: 20 } the test passes
sansRegleMarche(ordres).executions[0]
// { achat: 'a1', vente: 'v1', prix: 10050, quantite: 25 } the test fails: mutant killed
Step by step: the mutant keeps the same candidate prices (9,950 and 10,050), hence the same equilibrium price; only priority changes. a1 and a2 are now two buys limited at 10,050, and a1, which arrived first, goes ahead. The test expects a2: it fails, the rule is protected.
Cost. A mutation tool creates dozens of mutants and reruns the whole suite for each: n mutants, n runs of the suite. Keep it for the core of the engine (sorting, tie-breaks, allocation), where an inverted condition costs the most.
Edge cases in a table
The edge cases of one rule share the same test; only the data changes. it.each writes them once:
it.each([0, -5, 2.5, NaN])('refuses the quantity %s', (quantite) => {
const c = new Carnet()
expect(() => c.passer({ id: 'x', sens: 'achat', quantite, limite: 10000 })).toThrow()
})
All four cases pass: the engine refuses these quantities. Write the same table for the limit, with -3, 10,000.5 and NaN: all three tests are red, because the engine accepts these limits (known limit no. 2 of the engine contract sheet). Keep them red or mark them as a known defect, but do not delete them: a deleted test no longer documents anything.
From memory, lesson 6: what is the difference between an example test and a property-based test? Which one kills a mutant?
Your turn
Read next: AMF, « Choisir et passer un ordre de bourse : ce qu’il faut savoir »
A question? Ask your teacher, or an AI assistant, giving it the title of this lesson.