Test the FizzBuzz Rule
Easy About 12 min Free, no account needed
You're given a correct FizzBuzz. Write the tests that prove every branch, and that would catch a broken one.
The brief
Anyone can write FizzBuzz. The harder skill, the one interviews and code reviews really probe, is proving it is correct. So this puzzle is flipped around: the implementation is already written and correct. Your job is to write the tests.
Here is the code under test:
public class FizzBuzz
{
// "Fizz" if divisible by 3, "Buzz" if by 5, "FizzBuzz" if by both, else the number.
public string Convert(int n)
{
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return n.ToString();
}
}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
FizzBuzzabove, every one of your tests must pass. A test that's wrong about correct behavior isn't a test, it's a bug. - Against a set of planted-bug "mutants", buggy versions of
FizzBuzzyou 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. A lazy suite that only checks one 'Fizz' passes step 1 but lets most mutants through. Think about which inputs exercise each branch of the rule on its own, including the combined multiple-of-15 case. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `FizzBuzz` 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.
// Think about every branch: plain numbers, multiples of 3, of 5, and of both.
public class Tests
{
public void A_multiple_of_three_is_Fizz()
{
Assert.Equal("Fizz", new FizzBuzz().Convert(9));
}
// TODO: add tests for plain numbers, multiples of 5, and multiples of 15.
}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
- Equivalence partitioning
- Cover each class of input and every branch with one deliberate case.
- 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 Vowel Counter Easy Free You're given a correct vowel counter. Write the tests that cover case, every vowel, and the letters that are not vowels.
- Test the Max-of-Three Easy Free You're given a correct max-of-three function. Write the tests that prove it considers all three arguments.
- Test the Palindrome Check Easy Free You're given a correct palindrome check that ignores case and punctuation. Write the tests that cover every input class.