Skip to content
Katabench
Try free

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:

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

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 Temperature above, every one of your tests must pass.
  2. 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

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

C# unit testing exercises →

What it practices

Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas