The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,317 other followers

Unittesting FizzBuzz

Posted by jpluimers on 2021/02/23

Keep a version history of how you approach the below TDD driven approach, then discuss it with one of your co-workers.

Note there is no “right” approach, though probably you will experience that some environments and approaches may lead to code that is better, for instance because it is:

  • easier to explain
  • shorter
  • more performant

The above 3 might points bite each other (;

Based on FizzBuzz: One Simple Interview Question – YouTube.

Write initial tests

Write unit tests for this unit under test in pseudo code:

type OutputMethod: method(string Value)
class FizzBuzzGame:
    method Construct(OutputMethod Value)
    method Process(int Value)

Unit test that captures Output with a certain set of Values, then tests for the four possible combinations:

  • number
  • Fizz
  • Buzz
  • FizzBuzz

and also ensures that the passed OutputMethod is being called once for each call to Process.

Extend the tests

Now extend the tests to cover the below multiples and all the permutations caused by the longer list. Think carefully about the permutations.

  • 3 -> Fizz
  • 5 -> Buzz
  • 7 -> Fuzz
  • 11 -> Bizz
  • 13 -> Biff

Write the unit under test

The least tricky bit should be this step.

Be sure to test the solution with your co-workers as well.

Non positive numbers.

Did you think about negative numbers?

Did you think about the number zero?

What would you do about them in the tests and unit under test?

–jeroen


Related

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: