Skip to content
Katabench
Try free

Test the Vowel Counter

Easy About 12 min Free, no account needed

You're given a correct vowel counter. Write the tests that cover case, every vowel, and the letters that are not vowels.

The brief

Counting vowels is a one-loop function, but it still has a set of letters, a case rule, and a tempting near-miss in 'y'. The counter here is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class VowelCounter
{
    // Counts a, e, i, o, u case-insensitively. 'y' is not a vowel.
    public int Count(string text)
    {
        int count = 0;
        foreach (char c in text)
        {
            char lower = char.ToLowerInvariant(c);
            if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') count++;
        }
        return count;
    }
}

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 VowelCounter above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken counters 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 a vowel from the set, ignore case, count 'y', or count every character. One lowercase word catches almost none of them. Cover uppercase, each vowel, a 'y', and a word with no vowels. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `VowelCounter` 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.
// Cover uppercase, each of the five vowels, and a word with no vowels (and with a 'y').
public class Tests
{
    public void A_word_with_two_vowels()
    {
        Assert.Equal(2, new VowelCounter().Count("hello"));
    }

    // TODO: add uppercase vowels, every vowel, a 'y', and a word with none.
}

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

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