The Hello World Kata
Tags: python, tdd, kata, tutorial
Published: 2017-08-09 04:37:52

---
title: The Hello World Kata
published: True
description: Ask yourself, 'How would I TDD Hello World?'
tags: Python, TDD, kata, tutorial
---
![hello world](https://thepracticaldev.s3.amazonaws.com/i/xubivkzqchmwo0h122kw.gif)

#### Hello World via Test Driven Development (TDD)

Pick a programming language (maybe the one you primarily use at work)... How hard/simple would it be to execute this kata?

I created a hello world python kata template: [Hello World Kata](https://github.com/DEV3L/python_katas/tree/master/kata_template/hello_world) 
![Hello World Kata Template](https://thepracticaldev.s3.amazonaws.com/i/7twfnvgp6vtdtntah9dt.png)

If you are already familiar with TDD, unit tests, and/or mocking, then this seems like an absurd exercise. Yet, this small program is enough to exercise each rule of TDD plus touch unit testing and mocking. These are the fundamental bases of TDD!

The intention of this article is to define, then walk through one solution to this kata.

# Description
### Expected Behavior
Create a program, which outputs to the console, 'hello world'.

### Constraint
[The three rules of TDD as Described by Uncle Bob](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd)
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

# Solution (Python3)

1: [Add test to assert hello_world file exists](https://github.com/DEV3L/python_katas/commit/b615a5e9e175f175cbdec53b8601cfd163b73a72)

Start by creating a test file **hello_world_test.py**. Assert nothing more than the file exits using an import statement. **1st + 2nd TDD Rule**

```python
import hello_world
```
> *test fails* - ModuleNotFoundError: No module named 'hello_world'

2: [Add hello_world.py file](https://github.com/DEV3L/python_katas/commit/b1c1275eb685368b86d9a3a9fa611ed6c03d0e45)

Create **hello_world.py** as an empty file. **1st + 3rd TDD Rule**

```python
# no code, just a new file
```

> *test passes*

3: [Add failing test with mock builtin.print](https://github.com/DEV3L/python_katas/commit/5b592e54220b85518867e43de6ca3e05ea5043c1)

The Python builtins print is called when the 'print' function is invoked. Update the test to **mock** this builtin... **2nd TDD Rule**

```python
from unittest.mock import patch


@patch('builtins.print')
def test_hello_world(mock_print):
    import hello_world
    mock_print.assert_called_with('hello world')
```

> *test fails* : AssertionError: Expected call: print('hello world')

4: [Print hello world](https://github.com/DEV3L/python_katas/commit/092ebd2408ac7f000ed097806f4806361bc4def2)

Write the feature code!!!

```python
print('hello world')
```

> *test passes*

5: Profit! (Done)

# Thoughts
Hello world is where a majority of software engineers begin at in any new language. In Python it is simply "print('hello world')".

Yet, one line of feature code is enough to expose the fundamentals of TDD!

Test Driven Development is a programming methodology that takes minutes to learn, but a career to master...

[In regards to TDD resistance](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd):
> Now most programmers, when they first hear about this technique, think: “This is stupid!” “It’s going to slow me down, it’s a waste of time and effort, It will keep me from thinking, it will keep me from designing, it will just break my flow.” **However, think about what would happen if you walked in a room full of people working this way. Pick any random person at any random time. A minute ago, all their code worked.**