https://en.wikipedia.org/wiki/Test-driven_development
Testing our code increases our confidence that the code behaves as we expect and ensures that changes to our code won't cause regressions. Writing and maintaining tests is hard work, so we should leverage all the tools at our disposal to make it as painless as possible.
Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases. This is opposed to software being developed first and test cases created later.
TDD cycle:
1. Add a test. [The developer must clearly understand the requirements.]
2. Run all tests and see if the new test fails.
3. Write the code. [The new code written at this stage is not perfect.]
4. Run tests. [All test cases now pass.]
5. Refactor code. [Cleaning up, removing duplication,
improving readability and maintainability.]
Repeat with new tests.
Tests should be automated and stateless.
All written code should be covered by at least one test.
Test code must work correctly for both positive and negative cases.
Test code should be treated with the same respect as production code.
TDD can lead to more modularized, flexible, and extensible code.
Integration tests are separate from the TDD unit tests.
Mock objects have the same interface as the real objects they mimic, allowing a client object to remain unaware of whether it is using a real object or a mock object.
There are tools in Python that support TDD (unittest, pytest, mock).