読み物Testing

Level: Basic

How to Write Effective Test Cases: Test Design Techniques Explained

Turn requirements into precise test cases, and choose inputs that find defects with equivalence partitioning, boundary values, decision tables, and state transitions.

Edeeson Opina8 min read

A Basic lesson card: lesson 3 of the Software Testing & Quality Engineering series, How to Write Effective Test Cases: Test Design Techniques Explained, with the Miso fox thinking.

What you’ll learn

  • Turn requirements into test conditions and test cases
  • Write precise expected results and assertions
  • Apply equivalence partitioning and boundary value analysis
  • Use decision tables and state transitions for complex rules
On this page
  1. Before you start
  2. From requirement to test case
  3. Assertions: checking the right thing
  4. Equivalence partitioning
  5. Boundary value analysis
  6. Parameterized tests
  7. Decision tables
  8. State transition testing
  9. Error guessing and experience
  10. How much is enough? Coverage
  11. Common mistakes and troubleshooting
  12. Practical exercise: design the tests
  13. Key takeaways
  14. References
  15. What's next

Exhaustive testing is impossible, so every test suite is a selection. The difference between a weak suite and a strong one is rarely the number of tests; it's whether the chosen inputs are the ones most likely to reveal defects. This lesson covers how to turn requirements into test conditions and test cases, write clear expected results, and use the classic test design techniques (equivalence partitioning, boundary value analysis, decision tables, and state transitions) to choose inputs systematically, with a parameterized test you can run.

Before you start

You should know the testing levels from Understanding Testing Levels: Unit, Integration, System, and End-to-End Testing. The techniques here work for manual and automated tests at any level; the code examples use TypeScript and Vitest, which lesson 4 sets up from scratch.

From requirement to test case

Testing starts from what the software should do. Take this requirement for a cooking school's booking form:

A booking is for a group of 1 to 8 people. Members get 10% off orders of 5,000 cents or more.

Work from it in three steps:

  1. Test conditions: what needs to be checked. "Valid group sizes are accepted," "group sizes outside 1 to 8 are rejected," "members get the discount at or above 5,000 cents."
  2. Test cases: specific inputs, preconditions, and expected results for each condition.
  3. Test procedures: the steps to run them, manually or as automated code.

A well-written test case has:

  • an ID or name that says what it checks;
  • preconditions: the state before the test (signed in as a member, class has free seats);
  • input or steps;
  • the expected result, stated precisely, decided *before* running the test.
Text
ID:            BOOK-07
Title:         Rejects a group of 9 people
Precondition:  Class "Ramen basics" has 12 free seats
Steps:         Enter group size 9, submit the booking form
Expected:      Booking is not created; message "Groups are 1 to 8 people" is shown;
               free seats remain 12

That last line matters. A good expected result checks the important side effects too, not only the message: the seats shouldn't change either.

Assertions: checking the right thing

In automated tests, the expected result becomes an assertion. Assert on the outcome users care about, as precisely as the requirement allows:

  • expect(total).toBe(4500) is precise; expect(total).toBeLessThan(5000) would also pass if the discount were wrong.
  • Check one behavior per test, so a failure tells you exactly what broke.
  • Avoid asserting on incidental details, such as internal variable names or the exact wording of a log message, unless they're part of the requirement. Those assertions break when code is improved without changing behavior.

Equivalence partitioning

The first technique reduces the number of tests without losing much. Divide the inputs into partitions (equivalence classes) that the software should treat the same way. If one value in a partition works, the others probably do too; if one fails, the others probably fail the same way. Test at least one value from each partition.

For group size:

  • Invalid: below range: 0 and below.
  • Valid: 1 to 8.
  • Invalid: above range: 9 and above.
  • Invalid: not a whole number: 2.5.
  • Invalid: not a number: empty input, text, NaN.

Five partitions, five tests, instead of trying every number. Remember the invalid partitions; that's where many defects hide, because developers tend to test the paths they wrote on purpose.

Boundary value analysis

Defects cluster at the edges of partitions, where a developer writes < instead of <= or counts from zero instead of one. Boundary value analysis tests the values at and next to each boundary.

For the range 1 to 8, the boundaries are 1 and 8. Two-value boundary analysis tests each boundary and its closest neighbor outside the partition: 0, 1, 8, and 9. Three-value analysis adds the closest neighbor inside: 0, 1, 2, 7, 8, and 9.

The discount rule from lesson 1 has a boundary at 5,000 cents. The tests that matter most are 4,999 (no discount), 5,000 (discount), and 5,001 (discount). Lesson 4 shows that exactly this boundary test catches a real defect.

Parameterized tests

When many tests share the same shape, write the logic once and list the cases as data. Here are the partitions and boundaries for group size as one parameterized Vitest test:

party.test.tsTypeScript
import { describe, expect, it } from "vitest";
import { isValidPartySize } from "./party";

describe("isValidPartySize", () => {
  it.each([
    // [size, expected, why]
    [0, false, "just below the minimum"],
    [1, true, "the minimum"],
    [2, true, "just above the minimum"],
    [7, true, "just below the maximum"],
    [8, true, "the maximum"],
    [9, false, "just above the maximum"],
    [-3, false, "a negative number"],
    [2.5, false, "not a whole number"],
    [Number.NaN, false, "not a number at all"],
  ])("%s → %s (%s)", (size, expected) => {
    expect(isValidPartySize(size)).toBe(expected);
  });
});

Each row runs as its own test with a readable name:

Text
✓ isValidPartySize > 0 → false (just below the minimum)
✓ isValidPartySize > 1 → true (the minimum)
✓ isValidPartySize > 2 → true (just above the minimum)
✓ isValidPartySize > 7 → true (just below the maximum)
✓ isValidPartySize > 8 → true (the maximum)
✓ isValidPartySize > 9 → false (just above the maximum)
✓ isValidPartySize > -3 → false (a negative number)
✓ isValidPartySize > 2.5 → false (not a whole number)
✓ isValidPartySize > NaN → false (not a number at all)

The "why" column documents the design technique behind each case, so the next reader knows the suite isn't a random list of numbers.

Decision tables

When behavior depends on combinations of conditions, list the combinations in a decision table. Suppose the discount rules are: members get 10% off at 5,000 cents or more; a coupon gives 5% off any order; discounts don't stack, and the larger one wins.

Text
Rule                     1     2     3     4     5     6
Member?                  yes   yes   yes   no    no    no
Order ≥ 5,000 cents?     yes   yes   no    yes   no    any
Valid coupon?            yes   no    yes   yes   yes   no
Discount                 10%   10%   5%    5%    5%    0%

Each column is a test case. The table exposes questions the requirement didn't answer: what if a member's coupon is expired? Writing the table often finds defects in the requirement before any code exists.

State transition testing

Some behavior depends on history: what state the object is in. A booking moves through states:

Text
requested → confirmed → attended
     │           │
     └→ cancelled ←┘

State transition testing covers each valid transition (confirm a requested booking, cancel a confirmed one) and, just as importantly, invalid ones: can you confirm a cancelled booking? Can you cancel one that was already attended? Invalid transitions are a common source of defects, because nobody wrote code to prevent them.

Error guessing and experience

Formal techniques are complemented by error guessing: using experience to predict likely mistakes. Common suspects:

  • empty values, very long values, and whitespace-only values;
  • zero, negative numbers, and very large numbers;
  • names with apostrophes, accents, or non-Latin scripts;
  • dates around midnight, month ends, leap days, and daylight saving changes;
  • duplicate submissions, such as double-clicking a button;
  • concurrent actions, such as two people booking the last seat.

How much is enough? Coverage

Coverage measures how much of something your tests exercise:

  • Requirements coverage: which requirements have at least one test.
  • Technique coverage: whether each partition and boundary is tested.
  • Code coverage: which lines and branches ran during the tests (lesson 6).

Coverage tells you what's untested; it doesn't tell you the tests are good. A test that runs every line but asserts nothing has full coverage and no value. Lesson 13 shows mutation testing, which checks whether tests actually detect defects.

Common mistakes and troubleshooting

  • Only testing valid input. Every invalid partition deserves a test.
  • Skipping boundaries. Test at and next to every limit.
  • Vague expected results like "works correctly." State exactly what should happen, including side effects.
  • Deciding the expected result after running the test. You'll accept whatever the software did.
  • Copy-pasted tests. Use parameterized tests and keep the reason for each case.
  • Weak assertions that would pass even if the behavior were wrong.

If you can't decide the expected result for a case, you've found an ambiguity in the requirement. Ask before writing the test.

Practical exercise: design the tests

For the requirement "Passwords must be 15 to 64 characters":

  1. List the equivalence partitions, valid and invalid.
  2. Choose three-value boundary values for both limits.
  3. Write a parameterized test table with a "why" column (in Vitest, or in a spreadsheet for manual testing).
  4. Add three error-guessing cases.
  5. Write one full test case in the ID, precondition, steps, expected format, including a side effect to check.

Key takeaways

  • Turn requirements into test conditions, then into test cases with precise expected results decided in advance.
  • Equivalence partitioning picks one value per class of input, including invalid classes.
  • Boundary value analysis tests at and next to every limit, where defects cluster.
  • Decision tables cover combinations; state transition tests cover valid and invalid changes of state.
  • Coverage shows what's untested; it doesn't prove tests are effective.

References

What's next

Time to automate. Writing Your First Automated Unit Tests with TypeScript and Vitest sets up a project, writes a failing test, explains the failure, and fixes the code.

See every lesson in Software Testing & Quality Engineering: From Fundamentals to Production-Ready Software