A code kata is a fun way for computer programmers to practice coding. They are also used a lot for learning how to implement Test Driven Development (TDD) when writing code. One of the popular programming katas is called FizzBuzz. This is also a popular interview question for computer programmers.
The concept behind FizzBuzz is as follows:
- Write a program that prints the numbers 1-100, each on a new line
- For each number that is a multiple of 3, print “Fizz” instead of the number
- For each number that is a multiple of 5, print “Buzz” instead of the number
- For each number that is a multiple of both 3 and 5, print “FizzBuzz” instead of the number
Now that you know what you need to write, you can get started!
Creating a Workspace
The first step is to create a workspace or project folder on your machine. For example, you could create a katas folder with a fizzbuzz inside of it.
The next step is to install a source control program. One of the most popular is Git, but you could use something else like Mercurial. For the purposes of this tutorial, you will be using Git. You can get it from the Git website.
Now open up a terminal or run cmd.exe if you are a Windows user. Then navigate in the terminal to your fizzbuzz folder. You can use the cd command to do that. Once you are inside the folder, run the following command:
git init
This will initialize the fizzbuzz folder into a Git repository. Any files or folders that you add inside the fizzbuzz folder can now be added to Git and versioned.
The Fizz Test
To keep things simple, you can create your test file inside of the fizzbuzz folder. A lot of people will save their tests in sub-folder called test or tests and tell their test runner to add the top level folder to sys.path so that the tests can import it.
Note: If you need to brush up on how to use Python’s unittest library, then you might find Python 3 Testing: An Intro to unittest helpful.
Go ahead an create a file called test_fizzbuzz.py inside your fizzbuzz folder.
Now enter the following into your Python file:
import fizzbuzz import unittest class TestFizzBuzz(unittest.TestCase): def test_multiple_of_three(self): self.assertEqual(fizzbuzz.process(6), 'Fizz') if __name__ == '__main__': unittest.main()
Python comes with the unittest library builtin. To use it, all you need to do is import it and subclass unittest.TestCase. Then you can create a series of functions that represent the tests that you want to run.
Note that you also import the fizzbuzz module. You haven’t created that module yet, so you will receive a ModuleNotFoundError when you run this test code. You could create this file without even adding any code other than the imports and have a failing test. But for completeness, you go ahead and assert that fizzbuzz.process(6) returns the correct string.
The fix is to create an empty fizzbuzz.py file. This will only fix the ModuleNotFoundError, but it will allow you to run the test and see its output now.
You can run your test by doing this:
python test_fizzbuzz.py
The output will look something like this:
ERROR: test_multiple_of_three (__main__.TestFizzBuzz)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/michael/Dropbox/code/fizzbuzz/test_fizzbuzz.py", line 7, in test_multiple_of_three
self.assertEqual(fizzbuzz.process(6), 'Fizz')
AttributeError: module 'fizzbuzz' has no attribute 'process'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
So this tells you that your fizzbuzz module is missing an attribute called process.
You can fix that by adding a process() function to your fizzbuzz.py file:
def process(number): if number % 3 == 0: return 'Fizz'
This function accepts a number and uses the modulus operator to divide the number by 3 and check to see if there is a remainder. If there is no remainder, then you know that the number is divisible by 3 so you can return the string “Fizz”.
Now when you run the test, the output should look like this:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
The period on the first line above means that you ran one test and it passed.
Let’s take a quick step back here. When a test is failing, it is considered to be in a “red” state. When a test is passing, that is a “green” state. This refers to the Test Driven Development (TDD) mantra of red/green/refactor. Most developers will start a new project by creating a failing test (red). Then they will write the code to make the test pass, usually in the simplest way possible (green).
When your tests are green, that is a good time to commit your test and the code change(s). This allows you to have a working piece of code that you can rollback to. Now you can write a new test or refactor the code to make it better without worrying that you will lose your work because now you have an easy way to roll back to a previous version of the code.
To commit your code, you can do the following:
git add fizzbuzz.py test_fizzbuzz.py
git commit -m "First commit"
The first command will add the two new files. You don’t need to commit *.pyc files, just the Python files. There is a handy file called .gitignore that you can add to your Git repository that you may use to exclude certain file types or folder, such as *.pyc. Github has some default gitignore files for various languages that you can get if you’d like to see an example.
The second command is how you can commit the code to your local repository. The “-m” is for message followed by a descriptive message about the changes that you’re committing. If you would like to save your changes to Github as well (which is great for backup purposes), you should check out this article.
Now we are ready to write another test!
The Buzz Test
The second test that you can write can be for multiples of five. To add a new test, you can create another method in the TestFizzBuzz class:
import fizzbuzz import unittest class TestFizzBuzz(unittest.TestCase): def test_multiple_of_three(self): self.assertEqual(fizzbuzz.process(6), 'Fizz') def test_multiple_of_five(self): self.assertEqual(fizzbuzz.process(20), 'Buzz') if __name__ == '__main__': unittest.main()
This time around, you want to use a number that is only divisible by 5. When you call fizzbuzz.process(), you should get “Buzz” returned. When you run the test though, you will receive this:
F.
======================================================================
FAIL: test_multiple_of_five (__main__.TestFizzBuzz)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_fizzbuzz.py", line 10, in test_multiple_of_five
self.assertEqual(fizzbuzz.process(20), 'Buzz')
AssertionError: None != 'Buzz'
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
Oops! Right now your code uses the modulus operator to check for remainders after dividing by 3. If the number 20 has a remainder, that statement won’t run. The default return value of a function is None, so that is why you end up getting the failure above.
Go ahead and update the process() function to be the following:
def process(number): if number % 3 == 0: return 'Fizz' elif number % 5 == 0: return 'Buzz'
Now you can check for remainders with both 3 and 5. When you run the tests this time, the output should look like this:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Yay! Your tests passed and are now green! That means you can commit these changes to your Git repository.
Now you are ready to add a test for FizzBuzz!
The FizzBuzz Test
The next test that you can write will be for when you want to get “FizzBuzz” back. As you may recall, you will get FizzBuzz whenever the number is divisible by 3 and 5. Go ahead and add a third test that does just that:
import fizzbuzz import unittest class TestFizzBuzz(unittest.TestCase): def test_multiple_of_three(self): self.assertEqual(fizzbuzz.process(6), 'Fizz') def test_multiple_of_five(self): self.assertEqual(fizzbuzz.process(20), 'Buzz') def test_fizzbuzz(self): self.assertEqual(fizzbuzz.process(15), 'FizzBuzz') if __name__ == '__main__': unittest.main()
For this test, test_fizzbuzz, you ask your program to process the number 15. This shouldn’t work right yet, but go ahead and run the test code to check:
F..
======================================================================
FAIL: test_fizzbuzz (__main__.TestFizzBuzz)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_fizzbuzz.py", line 13, in test_fizzbuzz
self.assertEqual(fizzbuzz.process(15), 'FizzBuzz')
AssertionError: 'Fizz' != 'FizzBuzz'
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (failures=1)
Three tests were run with one failure. You are now back to red. This time the error is ‘Fizz’ != ‘FizzBuzz’ instead of comparing None to FizzBuzz. The reason for that is because your code checks if 15 is divisible by 3 and it is so it returns “Fizz”.
Since that isn’t what you want to happen, you will need to update your code to check if the number is divisible by 3 and 5 before checking for just 3:
def process(number): if number % 3 == 0 and number % 5 == 0: return 'FizzBuzz' elif number % 3 == 0: return 'Fizz' elif number % 5 == 0: return 'Buzz'
Here you do the divisibility check for 3 and 5 first. Then you check for the other two as before.
Now if you run your tests, you should get the following output:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
So far so good. However you don’t have the code working for returning numbers that aren’t divisible by 3 or 5. Time for another test!
The Final Test
The last thing that your code needs to do is return the number when it does have a remainder when divided by 3 and 5. Let’s test it a couple of different ways:
import fizzbuzz import unittest class TestFizzBuzz(unittest.TestCase): def test_multiple_of_three(self): self.assertEqual(fizzbuzz.process(6), 'Fizz') def test_multiple_of_five(self): self.assertEqual(fizzbuzz.process(20), 'Buzz') def test_fizzbuzz(self): self.assertEqual(fizzbuzz.process(15), 'FizzBuzz') def test_regular_numbers(self): self.assertEqual(fizzbuzz.process(2), 2) self.assertEqual(fizzbuzz.process(98), 98) if __name__ == '__main__': unittest.main()
For this test, you test normal numbers 2 and 98 with the test_regular_numbers() test. These numbers will always have a remainder when divided by 3 or 5, so they should just be returned.
When you run the tests now, you should get something like this:
...F
======================================================================
FAIL: test_regular_numbers (__main__.TestFizzBuzz)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_fizzbuzz.py", line 16, in test_regular_numbers
self.assertEqual(fizzbuzz.process(2), 2)
AssertionError: None != 2
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=1)
This time you are back to comparing None to the number, which is what you probably suspected would be the output.
Go ahead and update the process() function as follows:
def process(number): if number % 3 == 0 and number % 5 == 0: return 'FizzBuzz' elif number % 3 == 0: return 'Fizz' elif number % 5 == 0: return 'Buzz' else: return number
That was easy! All you needed to do at this point was add an else statement that returns the number.
Now when you run the tests, they should all pass:
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK
Good job! Now your code works. You can verify that it works for all the numbers, 1-100, by adding the following to your fizzbuzz.py module:
if __name__ == '__main__': for i in range(1, 101): print(process(i))
Now when you run fizzbuzz yourself using python fizzbuzz.py, you should see the appropriate output that was specified at the beginning of this tutorial.
This is a good time to commit your code and push it to the cloud.
Wrapping Up
Now you know the basics of using Test Driven Development to drive you to solve a coding kata. Python’s unittest module has many more types of asserts and functionality than is covered in this brief tutorial. You could also modify this tutorial to use pytest, another popular 3rd party Python package that you can use in place of Python’s own unittest module.
The nice thing about having these tests is that now you can refactor your code and verify you didn’t break anything by running the tests. This also allows you to add new features more easily without breaking existing features. Just be sure to add more tests as you add more features.
Related Reading
- Python’s unittest module documentation
- The pytest website
- Python Testing with doctest
- Python 3 Testing: An Intro to unittest
- Python 102: An Intro to TDD and unittest