Test the Grade Boundaries
Easy About 12 min Free, no account needed
You're given a correct score-to-letter grader. Write the boundary tests that pin every cutoff and catch an off-by-one.
The brief
This grader is already written and correct. Your job is the skill that separates a real test suite from a fake one: proving the boundaries are right by writing the tests.
Here is the code under test:
public class Grading
{
// A is 90+, B is 80-89, C is 70-79, D is 60-69, else F.
public string Grade(int score)
{
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "F";
}
}Write a Tests class. Each public, parameterless method is one test: it passes if it runs
without throwing and fails when an assertion throws. A tiny assertion library is in scope (no
using needed):
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);Your suite is run twice over:
- Against the correct
Gradingabove, every one of your tests must pass. - Against a set of planted-bug "mutants", buggy graders you never see. A mutant is caught when at least one of your tests fails against it.
You pass when your suite is green on the correct code and catches at least 4 of the 5 mutants.
Every mutant is a boundary bug: a cutoff that moved by a few points, or a >= that became a >. A
suite that only checks a comfortable 95 passes step 1 and catches none of them. This is boundary-value
testing: aim your inputs at the exact score where one grade becomes the next. Stuck? Reveal the model
suite to see what thorough looks like.
Starter code
// Write tests for the `Grading` class. Each public, parameterless method is one test; it passes
// if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy versions.
// The bugs live at the cutoffs (89 vs 90, 79 vs 80), so test the boundaries, not the middles.
public class Tests
{
public void An_excellent_score_is_an_A()
{
Assert.Equal("A", new Grading().Grade(95));
}
// TODO: test the exact cutoff of every grade (90, 80, 70, 60) and a failing score.
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
Related guides
What it practices
- Boundary value testing
- Pin behavior exactly at, just below and just above every boundary.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- Test the Clamp Function Easy Free You're given a correct clamp function. Write the tests that cover below, above, inside, and the boundaries.
- Test the Caesar Cipher Medium Free You're given a correct Caesar cipher. Write the tests that pin wraparound, case, non-letters, and shift normalization.
- Test the Password Policy Medium Free You're given a correct password validator with four rules. Write tests that isolate each rule so a dropped one is caught.
- Test the Triangle Classifier Medium Free You're given a correct triangle classifier. Write the tests that cover every shape and the invalid cases.