Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 50 ยท Quality, Testing and Delivery

Test Design Techniques

Learn how to choose good test cases: equivalence classes, boundary value analysis, decision tables, state-based tests and coverage, with runnable generators.

You can never test every possible input. A function that takes one 32-bit integer already has over four billion values, and real programs take many inputs at once. The skill of testing is therefore choosing a small set of cases that is likely to find the most defects. Test design techniques are systematic ways of doing that, so your tests do not depend on luck or on what you happened to think of that afternoon.

This lesson covers four black-box techniques (equivalence classes, boundary values, decision tables and state-based tests) and the idea of coverage. They work from the specification, which is why they apply to any language. The code samples are plain JavaScript stand-ins with small invented rules; they generate and run cases for you, but they are teaching examples, not a real test tool.

Four ways to pick cases

Equivalence classes Group inputs that behave alike.
Boundary values Test at the edges of each group.
Decision tables Cover every combination of rules.
State-based Test event sequences and transitions.

Use several together; each finds a different kind of bug.

1 / 2

Equivalence classes

Equivalence partitioning divides all possible inputs into groups (classes) in which the program should behave the same way. If one value of a class passes, you assume the others would too, so you test one representative per class instead of every value.

Take the rule "an applicant must be a whole number of years old, from 18 to 65 inclusive". The classes are:

Class Example Expected
Valid: 18 to 65 30 accepted
Invalid: below 18 10 rejected
Invalid: above 65 80 rejected
Invalid: not a whole number 30.5 rejected
Invalid: not a number "abc" rejected

Four or five tests now represent millions of inputs. The key skill is finding the classes, and the invalid ones are often forgotten: negative numbers, empty text, text that is too long, missing fields, wrong types. For outputs, you can do the same: partition by what the program is supposed to produce (for instance each discount tier) and make sure each is reached.

The assumption "all members behave alike" can be wrong, because it depends on how the code is written. Hence the next technique, which looks at where behavior is most likely to change.

Boundary value analysis

Experience shows that bugs cluster at the edges of classes, because that is where programmers write comparisons such as < or <=, and off-by-one errors live. Boundary value analysis adds tests at and around each boundary. For a valid range from min to max, the standard set has six values: min - 1, min, min + 1, max - 1, max and max + 1. Some teams use only the three nearest each edge; the idea is the same.

The stand-in below generates the boundary cases for a range, runs them against an age check, and compares each answer with the expectation.

JS Boundary case generator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

All six boundary cases pass. To see why the technique pays off, change age >= 18 to age > 18 in isValidAge and run again: only the line for 18 shows BUG, while a test using 30 would have passed. A single "typical" value cannot find that bug; a boundary value finds it immediately. Boundaries also exist beyond numbers: the first and last element of a list, an empty collection, the maximum length of a text field, midnight and the end of a month.

Decision tables

Some rules depend on combinations of conditions. A decision table lists every combination of conditions and the action each one should cause. It is both a specification tool (it exposes missing cases) and a test design tool (one test per column or row).

Imagine a shop rule: members get 10% off, and orders over 50 get free shipping. With two yes/no conditions there are 2 x 2 = 4 combinations:

Rule Member? Order over 50? Result
1 yes yes free shipping and 10% off
2 yes no 10% off
3 no yes free shipping
4 no no no benefit

With n yes/no conditions there are 2^n combinations, so tables grow quickly; with ten conditions you have 1,024 rows and need to simplify (merge rows where a condition does not matter, or split the rule). The next stand-in checks two things: the table is complete (has all 2^n rows) and the code agrees with every row.

JS Decision table check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

If you delete one rule from the table, the count no longer matches 2^n and the last line warns you. That is the second benefit: a decision table shows the combination nobody thought about.

State-based testing

Some systems behave differently depending on their history: an order can be shipped only after it was paid, and a shipped order cannot be cancelled. Such behavior is modeled as a state machine (see state, component and deployment diagrams): states, events and transitions. State-based test design derives tests from it:

  • Cover every state at least once.
  • Cover every valid transition at least once (stronger).
  • Test invalid events in each state: what happens if you try to ship an order that is still in the cart? The specification should say whether the event is rejected, ignored or an error.
  • For critical systems, test sequences of transitions (paths), not just single steps.

The stand-in below models an order as a table of transitions, walks three paths, and counts how many state and event pairs would still need an "invalid event" test.

JS State-based test paths

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Five states times four events give 20 pairs. Four are valid transitions, so 16 are invalid and each needs an expectation: ignored, rejected or error. Writing these down is where state-based testing finds specification gaps.

Coverage: measuring what you exercised

Coverage answers "how much of something did the tests touch?" The most common measures are white-box ones:

  • Statement coverage: the percentage of statements executed at least once.
  • Branch (decision) coverage: the percentage of if/else outcomes taken at least once. A test that always takes the true branch of an if can have full statement coverage of the body and still never test the other branch. Branch coverage is stronger.
  • Condition coverage: each atomic condition (a, b in a && b) is both true and false.
  • Requirements, state and transition coverage: the same idea applied to the specification, as in the stand-ins above.

Coverage tells you what you did not test, which is useful. It does not tell you that what you did test is checked well: a test that runs the code but asserts nothing gives 100% coverage and no protection. Treat a coverage number as a flashlight for finding gaps, not as a goal to hit (more in TDD and code quality practices).

Beyond the four techniques

  • Error guessing: use experience to try inputs that often break software: empty values, huge values, special characters, Unicode, duplicate submissions, time zones, leap days.
  • Exploratory testing: a person learns the product while testing it, designing the next test from what was just seen. It finds surprises that scripted tests cannot.
  • Pairwise (combinatorial) testing: when many options combine (browser, operating system, language), test every pair of values at least once, which needs far fewer cases than every combination and finds most defects.
  • Property-based testing: state a rule that must hold for all inputs ("sorting twice gives the same result as sorting once") and let a tool generate many random inputs to check it.

A good set mixes techniques: classes to find the groups, boundaries to probe the edges, tables for combinations, states for sequences, and exploration for what nobody foresaw.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A field accepts whole numbers from 1 to 100. Which set contains the boundary values of the valid range (and their neighbors)?

    Boundary analysis tests just below, on, and just above each edge: 0, 1, 2 near the lower edge and 99, 100, 101 near the upper edge.
  2. 2. What is the idea behind equivalence partitioning?

    If the program treats all members of a class the same way, one member is enough to represent the class, which reduces the number of tests.
  3. 3. A rule has 3 independent yes/no conditions. How many rows does a complete decision table have?

    Each condition doubles the combinations: 2 x 2 x 2 = 8.
  4. 4. A test suite gives 100% statement coverage. What can you conclude?

    Coverage measures what was executed, not whether results were checked or whether all behavior and branches were exercised.
  5. 5. In state-based testing, why test invalid events such as shipping an order that is still in the cart?

    Real users and other systems send unexpected events. The specified response (reject, ignore, error) must be tested, and writing it down often reveals missing rules.

Summary

  • You cannot test everything, so use techniques that pick few cases likely to find many defects.
  • Equivalence classes group inputs that behave alike (include the invalid classes); test one representative per class.
  • Boundary value analysis tests at and next to each edge, where off-by-one and comparison errors live.
  • Decision tables list all combinations of conditions (2^n for yes/no conditions) and expose missing rules.
  • State-based tests cover states, valid transitions and invalid events; coverage shows what was never exercised but not how good the checks are.

Next: TDD and Code Quality Practices.