Thursday 21 July 2016

Learning Python part 3: TDD and Fizzbuzz

Now everything is setup, I can run a program and it's related test, I am familiar with the syntax, it is time to begin writing some tested programs.

I began by completing the world famous fizzbuzz kata using TDD - Test Driven Development.

This helped me learn the following 

  1. get used to producing programs and tests in a systematic way via TDD
  2. Writing simple code to pass a test
  3. practise the syntax by solving problems
  4. read the stack trace to find out where the errors are
  5. apply simply refactoring to improve the design of my code
  6. Construct tests and implement the syntax for the testing framework
  7. Setup a project for code and tests


Fizzbuzz kata can be seen here...
https://en.wikipedia.org/wiki/Fizz_buzz

The aim is to produce a program that is passed a number and using the rules of fizzbuzz to produce the correct output.

My code is here...https://github.com/hanfak/fizzbuzz-python-tdd

If you want to see every step I made in producing the finished program and the tests, view the commit messages and start at the bottom, click on the first commit (best in new tab) and click on split view, and read the right hand side.  The only two files to read are game.py and app_game_tests.py, if you want to read the files alone go to browse file and search for them. Ignore the + or - signs, they just show all the new code and removed code.



To run the program



1. clone the repo to your hard drive, or download the zip file if you do not have access to GIT (really recommend using it)
2. Unzip and enter the folder fizzbuzz-python-tdd
3. run the tests using the commands in the read me ( I assume you have python set up, if not go to this post)
4. To see it in action for the first 100 numbers,  type python
5. load the file, type execfile('app/game.py')
6. type the following after each line press enter (dont include the >, just to show the input prompt)
               >fizzbuzz = FizzBuzz()
                   >for n in range(1, 100):
                    > print fizzbuzz.count(n), 

        7. press enter again and you will see the first 100 numbers with the correct outputs ie fizz etc.


        To run the tests:


        In the folder, type nosetests -v --rednose


        Refactoring


        The program is very simple, but can be refactored to make it much clearer and easier to read, as well as keeping the code clean. I applied encapsulation to the logic in the conditionals by creating a new private method (__divisible_by), this helped DRY out my code.


        Extra

        This was a simpe kata to get used to testing. If I was not very confident, I feel I would have taken another simple kata from here and implemented TDD. 

        Also doing these katas is also good way of practising for technical tests set by employers for prospective candidates. 

        Next...


        Next on the list is doing more advanced TDD (mocks, stubbing), using more python syntax and apply a few more design principles (SRP, injection).