Skip to content
Katabench
Try free

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:

C#
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):

C#
Assert.True(condition);          Assert.False(condition);
Assert.Equal(expected, actual);  Assert.NotEqual(a, b);
Assert.Null(x);                  Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);

How you're graded

Your suite is run twice over:

  1. Against the correct FizzBuzz above, every one of your tests must pass. A test that's wrong about correct behavior isn't a test, it's a bug.
  2. Against a set of planted-bug "mutants", buggy versions of FizzBuzz 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. 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

C#
// 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.

C# unit testing exercises →

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