Code Challenge 45 – TDD: Code FizzBuzz Writing Tests First!

By on 8 December 2017

It’s not that I’m so smart, it’s just that I stay with problems longer. – A. Einstein

Hi Pythonistas, it’s time to do things differently! Test Driven Development has to be one of the hardest programming methodologies to adopt yet also one of the most satisfying. It forces a different mindset which is definitely not for everyone. Either way, put your thinking caps on and jump in!

What is Test-Driven Development?

An excerpt from this cool TDD tutorial on Tuts+ sums it up nicely:

TDD, in its most basic terms, is the process of implementing code by writing your tests first, seeing them fail, then writing the code to make the tests pass. You can then build upon this developed code by appropriately altering your test to expect the outcome of additional functionality, then writing the code to make it pass again.

The Challenge

The challenge is to use TDD of course but to make what? Rather than leave this open ended, we’re going to ask you to use TDD while solving a kata featured in PyConEs 2016 Introduction to TDD Workshop (translation).

FizzBuzz is a simple problem wherein your program will print the numbers from 1 to 100. The tricky part is that if a number being printed is divisible by 3, the program returns “Fizz” instead of the number.

Likewise, print “Buzz” for any number divisible by 5 and “FizzBuzz” for any number divisible by 3 and 5.

It’s a very simple problem to solve which is why we choose it to focus on learning / practicing TDD!

Code a solution to the FizzBuzz problem using TDD and the challenge is complete. Make sure to PR your solution and your test file.

Doing it for Yourself

We’re aware that you can simply submit a PR with a test file you’ve created after writing your script but clearly that defeats the purpose of this challenge!

This challenge is intended to get you to try using TDD. There are no winners or losers so there really would be no point in writing the tests after! For that, feel free to try Code Challenge 39!

Pybites Slack

You like these challenges? We have published quite a few and we’re not planning to stop anytime soon.

You really like them and plan on PR’ing more in the future? Then consider joining our private Slack channel sending us an email. This way you get the unique opportunity to learn from other passionate Pythonistas and share some of your experience.

Credit

You can PR your work to our Community branch of our Challenges repo. We will include it in our review post. Our PR template also lets you reflect a bit on your learning and provide some feedback how we can keep our challenges interesting. See detailed instructions here.

About

Our goal is to learn and teach you Python through practical exercises. Learning a programming language is way more fun as a community!

For any feedback, issues or ideas use GH Issues, tweet us or drop us an email.


Keep Calm and Code in Python!

— PyBites

Want a career as a Python Developer but not sure where to start?