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
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.
Create a program, which outputs to the console, 'hello world'.
- 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.
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
test fails - ModuleNotFoundError: No module named 'hello_world'
Create hello_world.py as an empty file. 1st + 3rd TDD Rule
# no code, just a new file
The Python builtins print is called when the 'print' function is invoked. Update the test to mock this builtin... 2nd TDD Rule
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')
Write the feature code!!!
5: Profit! (Done)
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...
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.