Module: L1-M02 · Duration: ~150 min (estimate, including the independent exercise) · Previous: L02 · Next: L04 - Prompt sensitivity: one small change, a different result
1. Introduction
The six hand-written test cases in L02 all pass. The tester's question is: do those six cases represent what real users will send? A learner may send a correct sentence, a sentence with one error, a sentence with several errors, a long sentence, an off-topic question, a sentence containing an injection, or type in Vietnamese. They may be at level A1 or C1.
This lesson applies test design techniques you already know from traditional testing, namely equivalence partitioning, boundary value analysis and pairwise testing, to the input of an LLM feature. The result is a prompt test matrix, the main output of this module.
2. Learning objectives
- Identify the input factors and values of an LLM feature.
- Calculate the number of full combinations and the number of pairs to cover for a set of factors.
- Generate a pairwise matrix with code and verify that it covers every pair.
- Write an oracle for each input partition, and identify partitions that have no expected behaviour yet.
- Analyse the results of running the matrix to find defects that the hand-written test cases missed.
3. Prerequisites and tools
- L02: atomic requirements and oracles.
- Equivalence partitioning and boundary value analysis at a basic level. A reminder: split the input into groups that the system is expected to handle the same way, then test one representative of each group and the boundary points between groups.
4. Concepts
4.1 Factors and values for LLM input
A factor is a dimension along which the input can vary. Each factor has a few values, and each value is an equivalence partition.
| Factor | Value (partition) | Why this partition matters |
|---|---|---|
input_kind |
one_error, correct, two_errors, off_topic, injection, vietnamese | Each kind triggers a different branch of the instructions |
level |
A1, B1, C1 | The prompt asks for an explanation "suited to the level" |
length |
short, long | Long input is a boundary. You saw in L1-M01-L02 that context affects behaviour |
With LLMs, a factor that is very often forgotten is the language of the input: the instructions are written in English, but Vietnamese users may type in Vietnamese.
Everyday example. At a bubble-tea shop, every cup has a size (M, L), a sugar level (50%, 100%) and an ice level (less, normal). Each dimension is a factor, each choice is a value. A new employee does not need to make every possible cup to practise. But if they only ever practise size M, they will not know whether an L cup overflows.
Good vs bad. Choosing values for the length factor:
| Weak values | Good values | |
|---|---|---|
| What is chosen | Only short sentences of 5-8 words, "because they are easy to write" | One short sentence (5-8 words) and one long sentence (15-20 words), like the lab's INPUTS table; plus the boundaries: an empty input, and if the specification has a length limit (for example 500 characters), inputs of 500 and 501 characters |
| Why | Every case falls into the same partition | Each partition has a representative, and the boundaries between them are tried |
4.2 Combinatorial explosion
The number of full combinations is the product of the number of values of every factor: 6 × 3 × 2 = 36. Add one factor with 2 values and it becomes 72; five factors with 3 values each give 3⁵ = 243. With an LLM, each combination may also need to be run repeatedly (L1-M01-L03), so the cost multiplies quickly.
Everyday example. A bubble-tea shop with 3 sizes, 5 sugar levels, 4 ice levels and 10 toppings has 3 × 5 × 4 × 10 = 600 different cups. No shop makes all 600 before it opens.
4.3 Pairwise testing
Pairwise (or all-pairs) testing chooses a set of rows such that every pair of values of any two factors appears at least once. Why this technique is effective: NIST research records that "most software bugs and failures are caused by one or two parameters", and that the number of failures needing three or more parameters to interact falls off progressively [R1]. NIST developed the ACTS tool for combinatorial testing [R1].
Important note: NIST's finding comes from traditional software. This lesson has no evidence that the same proportion holds for LLM behaviour. Treat pairwise as a way of sampling systematically, not as a guarantee.
The number of pairs to cover for the factors above: 6×3 + 6×2 + 3×2 = 36. A pairwise matrix cannot have fewer rows than the product of the two largest factors (6 × 3 = 18 rows), because each row contains exactly one pair of those two factors.
Worked example. A very small set of factors, to see all three numbers. Save the snippet below as
try_l03.py in the lab folder, run python try_l03.py, then delete the file:
from lab.matrix import all_combinations, pairwise, required_pairs
drink = {"size": ["M", "L"], "sugar": ["50%", "100%"], "ice": ["less", "normal"]}
rows = pairwise(drink)
print("combinations:", len(all_combinations(drink)))
print("pairs to cover:", len(required_pairs(drink)))
print("pairwise rows:", len(rows))
for row in rows:
print(" ", row)
Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions print the same):
combinations: 8
pairs to cover: 12
pairwise rows: 4
{'size': 'M', 'sugar': '50%', 'ice': 'less'}
{'size': 'M', 'sugar': '100%', 'ice': 'normal'}
{'size': 'L', 'sugar': '50%', 'ice': 'normal'}
{'size': 'L', 'sugar': '100%', 'ice': 'less'}
Work it out by hand to compare:
- Combinations: 2 × 2 × 2 = 8.
- Pairs to cover: there are three pairs of factors (size-sugar, size-ice, sugar-ice), each with 2 × 2 = 4 value pairs, a total of 4 + 4 + 4 = 12.
- Each row has three factors, so it holds exactly three pairs. Four rows × 3 = 12 pairs, just enough, with no pair repeated.
- Lower bound: the two largest factors both have 2 values, so at least 2 × 2 = 4 rows. The algorithm reaches exactly that bound.
Check any pair, for example (L, less): it is in row 4. You have replaced 8 cups with 4, and every pair of choices is still made at least once.
4.4 Oracles by partition
A matrix only produces inputs. To get tests, each row needs an expected behaviour. The practical way is to write oracles by partition:
input_kind |
Oracle (from the prompt's instructions) |
|---|---|
| one_error, two_errors | corrected is present and differs from the input; Vietnamese explanation, at most 2 sentences |
| correct | corrected is exactly equal to the input |
| off_topic | corrected is null |
| injection | Does not contain the system prompt |
| vietnamese | None yet: the prompt says nothing about Vietnamese input |
The last row is a finding, not your mistake: while designing the matrix, you found a partition that the specification leaves empty. Record it as a question for the product owner.
Everyday example. A test paper has several kinds of question: multiple choice is marked against a key, essays against a marking scheme. When a question has no key yet, the teacher does not guess a mark but asks the subject team.
Good vs bad. The same row of the matrix:
| Row without an oracle | Row with an oracle | |
|---|---|---|
| Row | MX-06 off_topic C1 long |
MX-06 off_topic C1 long |
| Check | Run it, read the output, it "looks fine" | required_keys and corrected_equals: None, pass or fail automatically |
A vietnamese row |
Guess a behaviour and write "pass" | Write NO ORACLE and send a question to the product owner; it does not count as a pass |
5. Technical deep dive
5.1 The greedy algorithm in the lab
lab/matrix.py uses a simple, deterministic approach:
- Compute the set of all pairs that must be covered (
required_pairs). - Repeat: among all combinations, choose the row that covers the most pairs not yet covered, add it to the matrix, then remove the pairs it covers.
- Stop when no pair is left.
This algorithm is easy to understand, but it does not guarantee the smallest set, and it is slow when the number of combinations is very large. Dedicated tools such as ACTS produce smaller sets and also support higher interaction strengths [R1].
5.2 From a matrix row to a test case
to_case(row, index) takes the representative text for the pair (input_kind, length) from the INPUTS table, then attaches
the oracle with expected_checks(). If a partition has no oracle yet, the function returns None and the demo prints
NO ORACLE.
6. The QA perspective
- What to test: every partition of every factor, every pair of partitions across two factors, and the boundaries (long input, empty input, input in another language).
- Why: hand-written test cases tend to focus on whatever the author thinks of first.
- What can go wrong: a factor is missed (language, length); the representative values are too easy (short sentences only); the matrix has inputs but no oracles; believing that pairwise catches every defect.
- Expected behaviour: each partition has an oracle confirmed by the product owner. Any partition without one is reported as "not defined".
- How to detect it: run the matrix, then group the failures by factor. If every failure has
length = long, length is the lead. - How to report it: the matrix table with its results, the list of partitions without an oracle, and a hypothesis about the factor that causes the failure.
7. Real-world scenarios
Scenario 1 - LinguaBot (fictional; this is the lab). The 6 test cases all pass, but the matrix shows that a long sentence with grammar errors is returned unchanged. Nobody hand-wrote a long sentence with an error.
Scenario 2 - Appointment-booking chatbot for a clinic (fictional). Factors: language (vi, en), intent (book, cancel, reschedule, ask about prices, out of scope), channel (web, Zalo), time (during working hours, after hours). Full: 2 × 5 × 2 × 2 = 40. The algorithm in the lab produces 10 pairwise rows, exactly the lower bound 5 × 2. The pair "cancel × after hours" is the one for which the product team forgot to write an expected behaviour.
8. Hands-on tutorial
8.1 Running the matrix
python -m lab.matrix
Actual output (2026-10-31):
factors: input_kind=6, level=3, length=2
all combinations: 36
required pairs: 36
pairwise rows: 18 (uncovered pairs: 0)
for comparison, 5 factors x 3 values: 243 combinations, 15 pairwise rows
prompt v1 on the pairwise matrix:
MX-01 one_error A1 short pass
MX-02 one_error B1 long FAIL corrected is unchanged although the input has errors
MX-03 correct A1 long pass
MX-04 correct B1 short pass
MX-05 two_errors C1 short pass
MX-06 off_topic C1 long pass
MX-07 two_errors A1 long FAIL corrected is unchanged although the input has errors
MX-08 off_topic A1 short pass
MX-09 injection A1 short pass
MX-10 injection B1 long pass
MX-11 vietnamese A1 short NO ORACLE (ask the product owner)
MX-12 vietnamese B1 long NO ORACLE (ask the product owner)
MX-13 one_error C1 short pass
MX-14 correct C1 short pass
MX-15 two_errors B1 short pass
MX-16 off_topic B1 short pass
MX-17 injection C1 short pass
MX-18 vietnamese C1 short NO ORACLE (ask the product owner)
8.2 Reading the result
- 36 combinations, 36 pairs, 18 pairwise rows, and no pair is left uncovered. With this small set of factors, pairwise only halves the count, because 18 is already the lower bound (6 × 3). With 5 factors × 3 values, pairwise cuts 243 down to 15 rows. The benefit grows with the number of factors.
- MX-02 and MX-07 fail: both are long inputs with errors. The simulation returns the sentence unchanged. The 6 test cases from L02 had no long sentence with an error, so they missed it. (In the simulation, the cause is that the table of "known" sentences contains only short sentences. With a real model, the cause would be different, and only testing would tell.)
- MX-11, MX-12, MX-18 have no oracle: the prompt specifies nothing for Vietnamese input. This is a question for the product owner.
8.3 Running the tests
python -m pytest tests/test_matrix.py -v
Expected result: 7 passed. Two tests check properties of the matrix, not specific numbers:
def test_pairwise_covers_every_pair():
assert uncovered(pairwise(FACTORS), FACTORS) == set()
def test_pairwise_cannot_be_smaller_than_the_two_largest_factors():
assert len(pairwise(FACTORS)) >= 6 * 3
8.4 Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
KeyError in to_case |
A new value was added to input_kind or length without adding text to INPUTS |
Add text for every new (kind, length) pair |
| The demo runs very slowly | Too many factors: the greedy algorithm walks through every combination | Remove factors, or use a dedicated tool |
9. Independent exercise
Problem: The product team asks: should we test sentences both with and without a final full stop?
Requirements:
- Copy
exercises/test_l03_matrix.pyintotests/. Add apunctuationfactor with two valueswith_periodandwithout_period. - Record the new number of combinations and the new number of pairwise rows. Explain why the number of pairwise rows does not increase.
- Propose (in writing, no code needed) an oracle for the
vietnamesepartition: list 2 different behaviour options and the pros and cons of each, so that the product owner can choose.
Test data: FACTORS in lab/matrix.py plus the new factor.
Deliverable: a test file that asserts the two numbers correctly, and the written proposal.
Acceptance criteria: the test passes; the explanation states both points: the lower bound is still 6 × 3, and
the existing 18 rows have room for every new punctuation pair;
the proposal has 2 clear options, each of which can be written as an oracle.
Hint: the lower bound is still 6 × 3.
The Python you need: {**FACTORS, "punctuation": ["with_period", "without_period"]} creates a new
dict with every key of FACTORS plus one more key. {**d} means "copy every key-value pair
of d". The original dict is not modified.
9.1 Extra exercise: count the pairs and the lower bound before you generate a matrix
Time: 15-30 minutes. File: exercises/test_l03_extra_counts.py.
Problem: Before generating a matrix, a tester should be able to state two numbers: how many pairs the matrix must cover, and the fewest rows it can have. Those two numbers let you check a tool's result, and estimate the cost before you run anything.
Requirements:
- Copy the file into
tests/and runpython -m pytest tests/test_l03_extra_counts.py. Untouched, 8 tests fail with a message that starts withFailed: TODO. - Write
count_pairs(factors): for every pair of factors, multiply their numbers of values, then add the products up (§4.3). - Write
lower_bound(factors): the product of the numbers of values of the two largest factors, whatever order they are declared in. - Run again until you get
8 passed. What is thelargest-lastexample (2, 3 and 7 values) there for? Write one sentence.
Acceptance criteria: 8 tests pass; count_pairs gives the same result as the lab's
len(required_pairs(...)); the answer to step 4 says that the largest factor is not always declared
first.
Hint: itertools.combinations(factors, 2) gives every pair of factor names; sorted(..., reverse=True) orders the numbers of values from largest to smallest.
9.2 Real-world scenario: a parcel-tracking chatbot for a delivery company (fictional)
The delivery company "Giao Lien" (fictional) runs a chatbot in its app and on Zalo. Customers type in
Vietnamese or English, to track a parcel, change the address, complain, or chat about something
off-topic. A valid tracking code has exactly 12 characters and starts with GL. The product owner
wants to know: how many tests are needed, and which ones should run first?
Your task (45-60 minutes):
- List the factors and values. There are at least these factors: language, intent, tracking-code status, channel.
- Work out the number of combinations, the number of pairs to cover, and the pairwise lower bound. If
you like, check them with
lab/matrix.py. - Choose boundary values for the tracking code.
- Write an oracle for each intent, and point out at least one combination for which the specification does not say what should happen.
- Write a 3-5 sentence recommendation for the product owner: what to run first, and why.
A good answer has:
- Four factors with the numbers of values given in the brief (2, 4, 3, 2), with the tracking code split into partitions (valid, wrong format, no code)
- Three numbers (combinations, pairs, lower bound) worked out by hand and checked again with
lab/matrix.py - Boundary values for the tracking code: 11, 12 and 13 characters; the right length but not
starting with
GL - An oracle for each intent that can be written as a check (for example: off-topic means a polite refusal with no invented parcel status), and at least one combination with no expected behaviour recorded as a question (for example: changing the address with a badly formatted tracking code)
- A recommendation that says pairwise is a sampling method, not a guarantee, and runs the high-risk rows repeatedly first (address change, complaint)
10. Knowledge check
0/17 done · Results are kept in this browser only.
P02 · True or false
P03 · Multiple choice
P04 · Short answer
Why is "the language of the input" an important factor for a chatbot for Vietnamese users?
P05 · Short answer
Every failing row has length = long. What do you write in the report, and what is the next investigation step?
P06 · Fill in the blank
P07 · Multiple choice
P08 · Spot the bug
The expected_checks function below is copied from lab/matrix.py but has one defect. Find it.
def expected_checks(input_kind, text):
base = {"required_keys": ["corrected", "explanation"]}
if input_kind in ("one_error", "two_errors"):
return {**base, "corrected_differs_from": text, "explanation_in_vietnamese": True, "max_sentences": 2}
if input_kind == "correct":
return {**base, "corrected_equals": text}
if input_kind == "off_topic":
return {**base, "corrected_equals": text}
if input_kind == "injection":
return {"must_not_contain": ["You are LinguaBot", "<instructions>"]}
return NoneP09 · Put in order
P10 · Match
P11 · Recall
Lesson: L1-M02-L02P13 · Multiple choice
P14 · True or false
P15 · Fill in the blank
P16 · Select all that apply
P17 · Apply now
Do the extra exercise in §9.1: copy exercises/test_l03_extra_counts.py into tests/, run python -m pytest tests/test_l03_extra_counts.py once before editing it, write count_pairs and lower_bound, then run it again.
11. Summary
- Concepts: factor, value, partition; combinations and pairs; pairwise is a way of sampling systematically, not a guarantee; oracles by partition.
- Skills: list the factors; calculate the number of combinations and pairs; generate and verify a pairwise matrix; read the results by factor.
- Traps: forgetting the language and length factors; a matrix with no oracles; treating NIST's finding as a guarantee for LLMs.
- You can now: build a prompt test matrix for an LLM feature and explain why it represents real input.
12. References
| ID | Title | Publisher | URL | Accessed | Relevance |
|---|---|---|---|---|---|
| R1 | Automated Combinatorial Testing for Software (ACTS) | NIST CSRC | https://csrc.nist.gov/projects/automated-combinatorial-testing-for-software | 2026-10-31 | Failures are usually caused by 1-2 parameters; the ACTS tool |
Key terms (English ↔ Vietnamese)
| English | Tiếng Việt |
|---|---|
| Factor / value | Yếu tố / giá trị của yếu tố |
| Equivalence partitioning | Phân vùng tương đương |
| Boundary value | Giá trị biên |
| Combination | Tổ hợp đầy đủ |
| Pairwise / all-pairs testing | Kiểm thử theo cặp |
| Combinatorial explosion | Bùng nổ tổ hợp |
| Test matrix | Bảng tổ hợp test |