Test the Temperature Converter
Easy About 12 min Free, no account needed
You're given a correct Celsius-to-Fahrenheit converter. Write the tests that pin the formula.
The brief
A one-line formula still has more than one way to be wrong. This Celsius-to-Fahrenheit converter is already written and correct. Your job is to write the tests that prove it.
Here is the code under test:
public class Temperature
{
// F = C * 9 / 5 + 32 (whole degrees).
public int ToFahrenheit(int celsius)
{
return celsius * 9 / 5 + 32;
}
}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
Temperatureabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken converters 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. The mutants drop the offset, invert the ratio, use the wrong multiplier, or add before scaling. A single test value cannot separate those, so pick inputs that isolate the offset (try freezing point) from the scale (try boiling point). Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `Temperature` 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 formula has three parts (multiply, divide, add), so check values that exercise all of them.
public class Tests
{
public void Freezing_point_is_32()
{
Assert.Equal(32, new Temperature().ToFahrenheit(0));
}
// TODO: add the boiling point and a couple of other temperatures.
}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.
What it practices
- 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 FizzBuzz Rule Easy Free You're given a correct FizzBuzz. Write the tests that prove every branch, and that would catch a broken one.
- Test the Grade Boundaries Easy Free You're given a correct score-to-letter grader. Write the boundary tests that pin every cutoff and catch an off-by-one.