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:
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):
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
VowelCounterabove, every one of your tests must pass. - 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
// 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.
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 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 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.