Readings: Testing and Modules
In Tests We Trust - TDD with Python
- Test-Drive Development (TDD) is a test-first strategy for writing code.
- Tests are your alive documentation
- Follow same convention as module name (i.e.
test_module_name.py
) -
Arrange, Act, and Assert (AAA) structure:
- Arrange: you need to organize the data needed to execute that piece of code (input);
- Act: here you will execute the code being tested (exercise the behaviour);
-
Assert: after executing the code, you will check if the result (output) is the same as you were expecting.
pytest
is a good library to execute tests
There is a 3 step cycle to all this (quoted from article):
- Write a unit test and make it fail (it needs to fail because the feature isn’t there, right? If this test passes, call the Ghostbusters, really)
- Write the feature and make the test pass! (you can dance after that)
- Refactor the code — the first version doesn’t need to be the beautiful one (don’t be shy)
It is all about thinking about the software design more thoroughly before we actually jump into the coding process.