Parameterized tests and edge cases
Parameterized tests and edge cases
Parameterized tests run the same behavior check with several explicit inputs and expected results. Select cases from distinct behavior regions and their boundaries rather than testing many arbitrary values. For the fee rule, 0 to 49 costs 5, 50 and above costs 0, and negative totals must be rejected.
Proposed baseline: .NET SDK 10.0.404, net10.0 / C# 14. All results are reasoned, not executed. Build and test examples require an approved isolated environment. Framework examples are separate test classes, not console entry points.
Run a table of boundary cases without a framework
The console fixture checks five representative valid inputs. It throws immediately on a mismatch.
using System;(int Total, int Expected)[] cases = [(0, 5), (1, 5), (49, 5), (50, 0), (51, 0)];foreach (var entry in cases){ int actual = Fee(entry.Total); if (actual != entry.Expected) throw new Exception($"Failed for total {entry.Total}");}Console.WriteLine($"{cases.Length} cases passed");static int Fee(int total) => total < 50 ? 5 : 0;The table detects a threshold shifted in either direction. A separate invalid-input test is required because the simplified Fee function in this fixture assumes a nonnegative input.
Expected output (reasoned, not executed):
5 cases passedUse Theory and InlineData
In a compatible xUnit.net v3 test project, each InlineData row supplies arguments to the theory. Here the function has the same valid-input contract as the previous fixture.
using Xunit;public class FeeBoundaryTests{ [Theory] [InlineData(0, 5)] [InlineData(49, 5)] [InlineData(50, 0)] [InlineData(51, 0)] public void Fee_ValidTotal_MatchesRule(int total, int expected) { int actual = Fee(total); Assert.Equal(expected, actual); } private static int Fee(int total) => total < 50 ? 5 : 0;}The data are specification examples, not random coverage. A test runner may format individual results differently; discovery must be verified.
Expected observation (reasoned, not executed):
Four supplied data rows should pass when discovered and executed.Verify that a plausible wrong solution is caught
The deliberately incorrect alternative uses <= instead of <. This is a package-free model of a mutation-like test, not a mutation testing tool.
using System;(int Total, int Expected)[] cases = [(49, 5), (50, 0), (51, 0)];int mismatches = 0;foreach (var entry in cases){ int wrong = entry.Total <= 50 ? 5 : 0; if (wrong != entry.Expected) { Console.WriteLine($"Caught at {entry.Total}"); mismatches++; }}Console.WriteLine($"Mismatches: {mismatches}");The equality boundary exposes the mutation. A suite containing only 49 and 51 would miss it. This reasoning does not substitute for actual mutation analysis or framework execution.
Expected output (reasoned, not executed):
Caught at 50Mismatches: 1Common mistakes and edge cases
Do not use a huge table of equivalent cases instead of meaningful boundary coverage. Separate invalid inputs from valid results; a negative total should not be silently accepted by a production fee calculator. Data types must match the test method arguments. NUnit TestCase and MSTest DataRow are alternatives in their own frameworks; never mix their attributes into xUnit tests.
Practice and recap
The threshold changes from 50 to 100. Choose a minimal valid boundary set and one invalid case, with expected results.
Hint: Test zero, just below the threshold, the threshold, just above it, and a negative amount.
Learning objective: Select equivalence partitions and boundary inputs. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Parameterized tests and edge cases — knowledge check
Apply the lesson concepts to the following scenarios.
Which input catches <=50 instead of <50?
Which xUnit attribute marks a data-driven test?
Why include a negative input case?
Which is a better oracle for expected fees?
What does detecting one wrong implementation prove?