FizzBuzz for Testers

FizzBuzz?

The ‘Fizz-Buzz test’ is a coding interview question that is normally used to filter out developers. The ever-increasing rise of the SDET (software developer in-test) as testing shifts to the left means this question is now often being used when interviewing testers.

The statement of the problem is as follows:
“Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.”

The following post will outline the way I have decided to tackle the issue, coming from a testing background. I will be using Java and JUnit but the basic principles can be applied to most programming languages.

Understanding The problem

I decided to break the problem down into manageable bullet points, they are as follows:

1. Print numbers on screen from 0 to 100
2. For multiples of 3, replace the number with the word Fizz
3. For multiples of 5, replace the number with the word Buzz
4. For numbers that are both multiples of 3 and 5, replace the number with the word FizzBuzz

Below is what we expect from the output once the program has been written, essentially our acceptance criteria:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
12
13
14
FizzBuzz

Print numbers on screen from 0 to 100

So let’s start by creating a very simple class that contains a single method, that takes a number as an argument and simply returns that number, so it can then be displayed on-screen.

public class FizzBuzz {

public String fizzBuzzCalculator(int value) {

return String.valueOf(value);
}
}

I started out by creating a class that will be containing our method. The FizzBuzzCalculator method takes an integer value and simply returns it as a string. Later on, this method will decide if it is to return a number or any of our chosen words such as ‘Fizz’ or ‘Buzz’

The next step is to utilise this method to actually print our values to the screen. For now, if we ignore the logic around ‘FizzBuzz’. We simply need to print from 0 to 100. So let’s create another class with a main method that utilised our FizzBuzz calculator within a simple for-loop.

public class FizzBuzzApplication {

public static void main(String[] args) {

FizzBuzz fizzBuzz = new FizzBuzz();

for (int i = 1; i <= 100; i++) {

System.out.println(fizzBuzz.fizzBuzzCalculator(i));
}
}
}

On line 5 we create an instance of our FizzBuzz object which will then allow us to utilise the method that we created earlier.

Line 7 then initiates a for-loop with line 9 finally calling our method, whilst passing in the value of our number. From what we know of our method, the number that is passed in is simply returned as a string.

Running this application from the main method will simply print 0-100 within the console. We have successfully implemented the first part of our acceptance criteria.

For multiples of 3 replace the number with the word ‘Fizz’

The FizzBuzzCalculator method within the FizzBuzz class is where we will be placing our conditional logic. For our initial condition, we want multiples of 3 to be replaced by the word ‘Fizz’ whilst everything else still prints out the number.

The code below adds an IF condition into our FizzBuzzCalculator method

public class FizzBuzz {

public String fizzBuzzCalculator(int value) {

if (value % 3 == 0) {
return "Fizz";

} else {
return String.valueOf(value);
}
}
}

When our for-loop now passes numbers into this method, if it can be divided into 3 with no remainder (multiple of 3) then the word ‘Fizz’ will be returned as a string, otherwise, simply return the number as we were doing previously.

The following output is returned once we run the application:

1
2
Fizz
4
5
Fizz
7
8
Fizz
10
11
Fizz
13
14
Fizz

For multiples of 5 replace the number with the word ‘Buzz’

For the next part, we will be adding another conditional. From line 9 onwards we have simply used the same logic as ‘multiples of 3’ and replaced the value with 5. As before, if the integer does not meet the first two conditions then output the original value of the number that was passed in.

public class FizzBuzz {

public String fizzBuzzCalculator(int value) {

if (value % 3 == 0) {

return "Fizz";

} else if (value % 5 == 0) {

return "Buzz";

} else {

return String.valueOf(value);
}
}
}

When running the application, the following output will be returned.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz

For multiples of both 3 and 5, replace the number with the word ‘FizzBuzz’

Adding a thirst condition allows us to check for the third requirement. By adding lines 3 to 7 we are now checking for multiples of both 3 and 5. You might have noticed that we placed the final conditional ahead of the other 2. This has been done as these values can be either a multiple of 3 or 5. As a result, they would print either ‘Fizz’ or “Buzz’ if they were caught by those conditionals first. Placing the final conditional first eliminates that problem.

public class FizzBuzz {

public String fizzBuzzCalculator(int value) {

if (value % 3 == 0 && value % 5 == 0) {

return "FizzBuzz";

} else if (value % 3 == 0) {

return "Fizz";

} else if (value % 5 == 0) {

return "Buzz";

} else {

return String.valueOf(value);
}
}
}

The snippet below shows the output of running the application.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Everything up until now has been focused on the development. This final section provided some insight into the testing one would expect to do. So using our above acceptance criteria I decided on the following test cases.

1. Check 5
2. Check 3
3. Check 3 and 5
4. Check a plain value (not a ‘Fizz’ ‘Buzz’ of ‘FizzBuzz’ value)
5. Check Zero
6. Check a high boundary value

A JUnit test was created for each scenario, with our FizzBuzz object being instantiated within each test, followed by the calling of our FizzBuzzCalculator method. An integer is passed in, based on the test requirements and an assertion is made against what is returned.

public class FizzBuzzTests {

@Test
public void checkThree() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(3).equalsIgnoreCase("Fizz"));
}

@Test
public void checkFive() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(5).equalsIgnoreCase("Buzz"));
}

@Test
public void checkFiveAndThree() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(15).equalsIgnoreCase("FizzBuzz"));
}

@Test
public void checkPlainValue() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(1).equalsIgnoreCase("1"));
}

@Test
public void checkZero() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(0).equalsIgnoreCase("FizzBuzz"));
}

@Test
public void checkHighValue() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(101).equalsIgnoreCase("101"));
}
}
public class FizzBuzzTests {

@Test
public void checkThree() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(3).equalsIgnoreCase("Fizz"));
}

@Test
public void checkFive() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(5).equalsIgnoreCase("Buzz"));
}

@Test
public void checkFiveAndThree() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(15).equalsIgnoreCase("FizzBuzz"));
}

@Test
public void checkPlainValue() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(1).equalsIgnoreCase("1"));
}

@Test
public void checkZero() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(0).equalsIgnoreCase("FizzBuzz"));
}

@Test
public void checkHighValue() {

FizzBuzz fizzBuzz = new FizzBuzz();
Assert.assertTrue(fizzBuzz.fizzBuzzCalculator(101).equalsIgnoreCase("101"));
}
}

As expected, below is the result of running the tests:

One Reply to “FizzBuzz for Testers”

1. Mo says:

Well written with a simple yet effective example.