Boundary value analysis: choosing tests at the edges
Boundary value analysis picks test inputs on both sides of every partition edge. See it catch off-by-one mutants that full branch coverage lets through.
Five tests, every branch executed, a green run, and seven of eight planted bugs survive. Those are the numbers from the small experiment below, and the suite that produced them looks perfectly reasonable in review. It tests a late fee for a library system, where the rules are simple enough to fit on a sign at the front desk.
public static decimal LateFee(int daysOverdue)
{
if (daysOverdue < 0) throw new ArgumentOutOfRangeException(nameof(daysOverdue));
if (daysOverdue == 0) return 0m;
if (daysOverdue <= 7) return 5m;
if (daysOverdue <= 30) return 15m;
return 40m;
}That suite takes one representative value from each kind of input: -5, 0, 4, 19 and 60. This is equivalence partitioning, and it is the right place to start. Inputs in the same partition should behave the same way, so testing 19 stands in for testing 12 or 25. The trouble is what it leaves out. Every value in that suite sits far from the lines that separate one fee from the next.
Boundary value analysis adds the missing step: find the edges of each partition, then test on both sides of each edge.
Same five partitions, two ways of choosing test inputs
Invalid
below 0 days: throws
Middle: -5
Edges: -1
On time
exactly 0 days: no fee
Middle: 0
Edges: 0
Short
1 to 7 days: fee 5
Middle: 4
Edges: 1 and 7
Medium
8 to 30 days: fee 15
Middle: 19
Edges: 8 and 30
Long
31 and up days: fee 40
Middle: 60
Edges: 31
One value per partition: 1 of 8 mutants caught
Every branch runs, but no input sits next to a line, so moving a line changes nothing the suite can see.
Both sides of each line: 8 of 8 mutants caught
Seven inputs pin all four lines. A line moved by one day flips the result for one of them.
Eight mutants of LateFee, each one a small slip in a comparison, put numbers on the difference. The
boundary suite uses -1, 0, 1, 7, 8, 30 and 31. Results from running both suites, lightly reformatted:
d < 0 -> d <= 0 midpoints: caught boundaries: caught
d < 0 -> d < -1 midpoints: SURVIVED boundaries: caught
d == 0 -> d <= 1 midpoints: SURVIVED boundaries: caught
d <= 7 -> d < 7 midpoints: SURVIVED boundaries: caught
d <= 7 -> d <= 8 midpoints: SURVIVED boundaries: caught
d <= 30 -> d < 30 midpoints: SURVIVED boundaries: caught
d <= 30 -> d <= 31 midpoints: SURVIVED boundaries: caught
d <= 30 -> d <= 29 midpoints: SURVIVED boundaries: caught
midpoints caught 1/8; boundaries caught 8/8Seven inputs caught all eight mutants where five caught one. The eight are a sample chosen to make a point, so read 1/8 as an illustration of this function, not a general rate. The mechanism does generalize, though: a comparison that moves by one only changes the answer for an input right next to it. That is also why the coverage report offered no warning, since the midpoint suite runs every branch. Green checkmarks tell you code ran, and only an assertion placed where a change would show tells you the behavior is pinned.
Five inputs, one from each partition, caught 1 of 8 mutants. Seven inputs on both sides of each line caught all 8.
In xUnit, the boundary suite reads like the rule it protects:
[Theory]
[InlineData(0, 0)]
[InlineData(1, 5)]
[InlineData(7, 5)]
[InlineData(8, 15)]
[InlineData(30, 15)]
[InlineData(31, 40)]
public void Fee_changes_exactly_at_each_boundary(int days, int expected) =>
Assert.Equal(expected, Fees.LateFee(days));
[Fact]
public void One_day_before_due_date_is_rejected() =>
Assert.Throws<ArgumentOutOfRangeException>(() => Fees.LateFee(-1));The ISTQB Foundation syllabus describes two versions of the technique, and it states a limit worth keeping in mind: the partitions have to be ordered, because "next to" means nothing for an unordered set of values such as country codes.
Two-value analysis tests each boundary value and its closest neighbor in the adjacent partition.
Three-value analysis tests the boundary value and both neighbors. The syllabus gives the case where
the extra value matters: if x <= 10 is wrongly written as x == 10, the inputs 10 and 11 both pass,
and only 9 exposes it. For most business rules two values are a good default. Move to three where a
comparison has more ways to go wrong than a shifted constant, such as an equality written in place of
a range, or a rule where a miss is expensive.
The fee schedule hands you its boundaries. Many others appear only when you ask what the code assumes.
Sizes have edges at empty, one element and exactly full. Text has them at the maximum length and one
past it. Numeric types have them at their limits, and the classic example is one line of arithmetic.
Joshua Bloch's 2006 write-up
describes (low + high) / 2 in a binary search overflowing once an array reaches about a billion
elements. Running the same arithmetic in C# with low = 1_500_000_000 and high = 1_600_000_000
gives a midpoint of -597483648, while low + (high - low) / 2 gives 1550000000. No test built from
"typical" inputs would find that.
Implementation details create boundaries too. A cache that switches strategy at 1,000 entries has an edge at 1,000 even though the requirements never mention it. Reading the code to find its constants and comparisons is fair game when you choose inputs, as long as your expected values still come from the rule, not the code.
A coupon is valid "through March 31". The obvious implementation compares against midnight at the start of that day:
static bool IsValidBuggy(DateTime now, DateTime lastDay) => now <= lastDay;
static bool IsValid(DateTime now, DateOnly lastDay) =>
now < lastDay.AddDays(1).ToDateTime(TimeOnly.MinValue);A test using new DateTime(2026, 3, 31) passes against both, because a date with no time is exactly
the one instant where the bug is invisible. Running the two at the real edges shows the difference:
2026-03-30 23:59:59 buggy=True fixed=True
2026-03-31 00:00:00 buggy=True fixed=True
2026-03-31 09:30:00 buggy=False fixed=True
2026-03-31 23:59:59 buggy=False fixed=True
2026-04-01 00:00:00 buggy=False fixed=FalseThe fix uses a half-open range, inclusive at the start and exclusive at the end, which leaves no gap whatever the clock's precision. The boundary test for it is the first instant after the range.
Calendars have edges that integers do not. DateTime.AddMonths
uses the last valid day when the target month is shorter, so January 31 plus one month is February 28
in 2026. Add another month to that result and you get March 28, while adding two months at once gives
March 31. February 29, 2028 plus one year lands on February 28, 2029. In the Europe/Berlin zone,
March 29, 2026 is 23 hours long and October 25, 2026 is 25 hours long, going by the difference
between the two midnights with their real offsets. Any rule involving "a month" or "a day" deserves tests at
month ends, leap days and clock changes, and it helps to control the clock
in those tests.
For money, the neighbor of 50.00 is 49.99, not 49. If orders of 50.00 and above ship free, those are your two values. Then look at what happens between the cents, because tax and discounts produce fractions.
Here Math.Round matters. Its default for midpoints
is rounding to the nearest even number, so Math.Round(2.345m, 2) returns 2.34, and passing
MidpointRounding.AwayFromZero returns 2.35. A subtotal of 49.995m displays as 50.00 yet fails an
exact >= 50m check, so a customer can see a total of 50.00 and still pay for shipping. Whether the
threshold applies to the exact or the displayed amount is a business decision. Make someone take it,
then write the test that names it.
Binary floating point adds its own edge. Executed, 49.90 + 0.05 + 0.05 as double gives
49.99999999999999 and fails the threshold, while the same sum in decimal gives exactly 50.00.
Allocation has one as well: 100.00 split three ways and rounded gives 33.33 each, which adds back up
to 99.99.
Choosing edges by hand depends on noticing them. Mutation testing gives you a second opinion. The
equality operator mutators
in Stryker.NET apply slips like those in the table above, turning a < b into a <= b and
a <= b into a < b. A surviving boundary mutant points to the edge you have not pinned. The
mutation testing post covers how to read those results, including
mutants that are equivalent and cannot be killed.
The eight mutants in this post were written by the same hand that chose the tests. In the Test Writing track the mutants stay hidden: "Test the Clamp Function" gives you three lines of correct code, and your suite passes only if it stays green there and catches at least 4 of 5 planted bugs. Similar exercises are collected under C# unit testing exercises.
Boundary tests have their own blind spot. They treat each input's edges separately, so a rule that only breaks when two inputs sit at their limits together can still slip through. Add a few combinations for rules that interact, and consider property-based tests when the input space is too large to list.
Practice what you just read
- Open in the editor: Test the Clamp Function
Test writing Easy Free, no account needed
Test the Clamp Function
You're given a correct clamp function. Write the tests that cover below, above, inside, and the boundaries.
More like this: C# unit testing exercises →