Part II — Intermediate Development · Chapter 10 · Lesson 1 of 6 · 7 min read
Lesson 1: Native Test Syntax and Expectations
Level: Intermediate
Purpose. Turn the informal prediction tables used so far into repeatable, automated tests. This lesson introduces how a PeaLang test is structured, how expectations are written, and how to keep tests small, deterministic, and diagnosable.
Learning objectives
After this lesson you will be able to:
- explain why automated tests replace manual comparison of printed output;
- write a test with a clear name, arrangement, action, and expectation;
- choose between value, boolean, error, and collection expectations;
- write failure messages that identify the cause quickly;
- organize tests alongside the modules they cover;
- run a test suite and read its result and exit status.
Release note: the test keywords, helper names, and commands in this chapter are illustrative. The language reserves the keyword assert, and the tooling layer includes testing, but verify the exact native syntax and commands against the installed v0.63 release before relying on them.
1.1 From prediction tables to automated tests
Earlier checkpoints asked you to predict a result, run the program, and compare. That process is valuable but manual: it depends on reading output carefully and repeating every case after every change. An automated test records the prediction in code, runs it on demand, and reports a pass or fail that a script or CI pipeline can act on.
1.2 Anatomy of a test
Every test has four parts, whatever syntax expresses them:
| Part | Question answered | Example |
|---|---|---|
| Name | What behavior is being checked? | gold order with bulk quantity gets combined rate |
| Arrange | What input and configuration are used? | a gold-tier order with items at the bulk threshold |
| Act | What single operation is run? | call the exported decision function |
| Assert | What must be true afterwards? | reason code is GOLD_BULK |
A test should have one reason to fail. If a test acts on several unrelated things, a failure does not say which one broke.
1.3 A first test (illustrative syntax)
test "gold bulk order gets combined reason" {
let order = {"order_id": "A1", "customer_tier": "gold", "subtotal": 100, "item_count": 10}
let result = decide_order(order)
assert result["reason_code"] == "GOLD_BULK", "expected GOLD_BULK"
}
The block has a descriptive name, one arranged input, one action, and one expectation with a message. The structure matters more than the exact keywords.
1.4 Expectations
An expectation states what must be true. Choose the most specific form that reads clearly.
| Kind | Use when | Illustration |
|---|---|---|
| Equality | A value must match exactly | assert result["discount"] == 10 |
| Boolean | A condition must hold | assert result["allowed"] |
| Negation | A condition must not hold | assert not result["allowed"] |
| Membership | An item must appear in a collection | assert "VALIDATION_ERROR" == result["reason_code"] |
| Error | A call must fail in a defined way | expect the call to raise a validation error |
| Tolerance | A float result must be close to a target | assert abs(actual - expected) < 0.0001 |
Prefer equality on a single named field over comparing a whole map at once; the failure then points at the field that differs.
1.5 Failure messages
A failing test is only useful if its message helps. State the expectation and, where it helps, the observed value.
- Weak: assertion failed.
- Better: expected reason GOLD_BULK for gold order with 10 items.
- Best when the tool allows it: include the actual value, such as got GOLD.
Never include secrets or personal data in a message; failure output is stored in logs.
1.6 Testing pure rules first
The pure rules module from Chapter 9 is the easiest and most valuable thing to test: no files, no network, no clock. Give it values and check results. Test adapters separately and sparingly, using injected substitutes for capabilities.
1.7 Naming and organizing tests
- Name a test for the behavior, not the function: invalid tier is rejected, not test_validate_2.
- Keep tests in a tests folder mirroring the source layout, for example rules_tests.pea for rules.pea.
- Group related tests in one file, and keep each file independent of the others.
- Do not let one test depend on another test having run first.
1.8 Boundary tests
Every threshold deserves three tests: just below, exactly at, and just above. For a bulk threshold of 10 items, test 9, 10, and 11.
test "nine items is not bulk" {
let result = decide_order(order_with(item_count: 9))
assert result["reason_code"] == "NONE", "9 items must not be bulk"
}
test "ten items is bulk" {
let result = decide_order(order_with(item_count: 10))
assert result["reason_code"] == "BULK", "10 items must be bulk"
}
The helper order_with is assumed to build a standard order and override one field; helpers like this keep tests short and make the changed field obvious.
1.9 Testing failure paths
Invalid input and error cases are part of the contract, so test them. Check both that the call is rejected and that the reason is the stable code, not merely that something went wrong.
1.10 Determinism
A test must give the same answer every time. Avoid reading the current time, using unseeded randomness, depending on file order, or relying on the network. Pass such values in as inputs so the test can fix them.
1.11 Running tests and reading results
pea test tests/
pea test tests/rules_tests.pea
Command form is illustrative; confirm it in the installed release. A run should report how many tests passed and failed, name each failure with its message, and return a nonzero exit status when anything fails, so a CI step stops on failure. In CI, run tests after the format check and the checker, as a separate step.
1.12 Common mistakes
| Mistake | Consequence | Correction |
|---|---|---|
| One test checks many behaviors | A failure does not say what broke | Split into focused tests |
| Vague failure message | Slow diagnosis | State the expectation and the input |
| Testing only the happy path | Validation bugs reach users | Add failure and boundary tests |
| Tests that depend on each other or on order | Intermittent failures | Make each test self-contained |
| Copying the implementation into the test | The test cannot catch a wrong rule | Write expected values from the policy, not from the code |
| Real network, clock, or files in rule tests | Flaky tests | Inject values and substitutes |
| Weakening a test to make it pass | Hidden defects | Fix the code or change the requirement deliberately |
1.13 Diagnostic workflow for a failing test
- Read the test name and message and restate the expectation.
- Re-run only that test.
- Check whether the arrangement matches the policy.
- Print or inspect the actual value.
- Decide whether the code or the test is wrong, using the written policy as the authority.
- Fix the smallest thing, then rerun the whole suite.
1.14 Guided practice
- Rewrite the sentence "gold orders get the gold rate" as a test with a name, arrangement, action, and expectation.
- Write three boundary tests for a user-count threshold of 100.
- Improve the failure message assertion failed for a priority test.
- Decide which expectation kind fits: a discount equals 10; an order is rejected; a rate is close to 0.15.
- Explain why a test should not copy the formula used by the code.
1.15 Hands-on lab: Test the Chapter 9 rules module
Goal: Write a small automated suite for the pure rules module of the multi-module project.
- Create rules_tests.pea in the tests folder.
- Write one test for each reason code: GOLD_BULK, GOLD, BULK, and NONE.
- Write below, at, and above tests for the bulk threshold.
- Write a test for the discount cap when the combined rate would exceed the subtotal.
- Write tests for the validation cases that return VALIDATION_ERROR.
- Give every assertion a message that states the expectation.
- Run the suite and record the count of passes and failures.
- Introduce one deliberate defect in the rules module, confirm that exactly the relevant tests fail, then remove the defect.
Deliverable: The test file, the run output, the defect note, and a table mapping each policy rule to the tests that cover it. Use synthetic data only.
1.16 Review questions
- Why is an automated test better than comparing printed output by eye?
- What are the four parts of a test?
- Why should a test have one reason to fail?
- Which expectation kind suits a float result?
- What makes a failure message useful?
- Why test the pure rules module before adapters?
- What are the three points to test at every threshold?
- Why must tests be deterministic, and how do you achieve it?
- What should a test run return so CI can stop on failure?
1.17 Applied challenge
Choose a rules module from an earlier chapter and write a test plan: list each policy statement, the test names that cover it, the boundary cases, the failure cases, and the inputs you will inject to keep tests deterministic. Then implement the plan and report which statements, if any, remain uncovered and why.
1.18 Lesson summary
Native tests turn predictions into repeatable checks. Each test has a clear name, an arrangement, a single action, and a specific expectation with a helpful message. Boundary and failure tests protect the contract, determinism keeps results trustworthy, and a nonzero exit status lets automation stop on failure. Begin with the pure rules module, where tests are simple and valuable.
Transition to Lesson 2
The next lesson covers filtering, watch mode, and coverage baselines: running only the tests you need, re-running them as you edit, and measuring what the suite exercises.
Lesson status: Ready for review.