This project is intended to give developers a simple starting point to practice Test-Driven Development (TDD) using katas. Learning to practice TDD is not like learning a framework or a library. When learning to use a library, you read the docs and sample code and then you're either able to use the library successfully, or you're not. TDD, on the other hand, is a practice: it is not about what you're doing as much as it is about how you're doing it.
Though TDD can apply to a number of different types of tests, this project focuses on applying TDD to writing unit tests. While unit testing often involves writing tests after the software is complete, TDD involves writing tests before writing the software that is to be tested. Another way of explaining this approach would be to say that unit tests and software are developed together, but the unit tests are often a step ahead of the software itself. If you're unfamiliar with TDD, the idea of writing tests before writing software might seem odd. Uncle Bob's "Three Rules of TDD" captures the workflow very succinctly:
- 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.
These are short. You should read them; I'll wait:
- Kata - the Only Way to Learn TDD The author lists a number of TDD katas in addition to discussing the practice.
- Uncle Bob's "Three Rules of TDD"
- TDD Test-driven development cycle from Wikipedia for another perspective on the "Three Rules"
The project features a minimal setup to allow you to quickly start practicing TDD katas using Python and pytest
.
reqiurements.txt
: Install the project's requirements/dependencies withpip install -r requirements.txt
.pytest.ini
: This configuration tellspytest
where to find both source and test files so.src/string_calculator.py
andtest/string_calculator_test.py
: Starter files for the String Calculator kata
All of these commands can be run at the root of the project.
pytest
: run all testspytest --cov=src
: run all tests and produce a test coverage report. This is certainly not necessary for learning TDD, but it can be interesting to understand how complete your unit tests are.ptw
: starts pytest-watch which will automatically run all tests any time a file in the project has changed. While this is not necessary for learning TDD, I have found that tools like this add to the enjoyment of practicing TDD.
Mocking libraries like mockito
have been intentionally excluded. While learning to leverage powerful mocking libraries is an important part of learning to write properly-isolated unit tests, that is not the goal of this project.
Clone or fork this project and make it your own! You can complete the starter String Calculator files, or you can delete them and create your own. Obviously, you can add files and start practicing other katas. If you're looking for more, this blog post contains a nice list; Google will help you find plenty more.