

Running python mytests.py again we get the following output in the command line. To ensure the code passes, lets change mycode.py to the following: def hello_world(): Fortunately, we have already written the tests, so we know that it will always be there to check this function, which gives us confidence in spotting potential bugs in the future. This clearly indicates that the test failed, which was expected. Running python mytests.py will generate the following output in the command line: F

In the file mycode.py we will initially just include the code below, which creates the function but doesn’t return anything at this stage: def hello_world(): Notice that we are importing helloworld() function from mycode file. Self.assertEqual(hello_world(), 'hello world') Let’s begin with the usual “hello world”: import unittest

To do this we create a new file mytests.py, which will contain all our tests. To begin writing tests in Python we will use the unittest module that comes with Python. General workflow of TDD How to get started? Tests also give you confidence when you begin to refactor code, as you are more likely to catch bugs due to the instant feedback when tests are executed. This also prevents the possibility of writing tests being postponed to a later date, as they might not be deemed as necessary compared to additional features that could be created during that time. Only when you are happy with your tests and the features it tests, do you begin to write the actual code in order to satisfy the conditions imposed by the test that would allow them to pass.įollowing this process ensures that you careful plan the code you write in order to pass these tests. In layman’s terms, TDD recommends writing tests that would check the functionality of your code prior to your writing the actual code. Turns out that this problem could be overcome by following a Test Driven Development (TDD) methodology. This situation is made worse if I come back to the code I’ve written after a few days. It’s impossible to remember how everything is interconnected in my head. I am a self-taught beginning developer who is able to write simple apps. By Dmitry Rastorguev A simple introduction to Test Driven Development with Python
