Unit testingΒΆ
Unit tests increase code quality and provide means to steer a project - they set objectives for next steps or help fix bugs. Unit tests mean writing more code and reviewing code under test, which takes time and effort.
Below are a few links and my quick notes about unit testing in Python.
1. Favourite video about testing?
TDD, Where Did It All Go Wrong by Ian Cooper is very impactful.
2. A trivial test example?
def inc(x: int) -> int:
return x + 1
assert inc(1) == 2
Are there property guarantees?
Does typing help?
Not suited for dependency injection, right?
3. Is unit test a proof?
Usually, no. You may try property testing for more guarantees.
4. What parts of code should I test?
At least public methods and bug fixes.
The smallest parts (through unit tests) and the biggest (through end-to-end, integration tests).
Beware of dirty hybrids [Sanderson(2009)], the costly tests that attempt to target things in between.
5. Does typing help writing a more testable code?
Yes, quite much. In Python we can then skip type-checking varieties of tests.
6. Should one mock or monkey-patch for testing?
Only if you cannot use dependency injection.
7. A continious integration (CI) for your projects?
I was a fan of Travis CI, but now Github Actions are a natural choice.
8. pytest?
Yes, pytest.
9. Difficult questions for you about unit testing?
10. Anything about other types of tests?
Unit tests are a part functional tests. There is more to test on a system in or near production, depending on your release cylce sophistication and handling of incidents. See chaos engineering, or fault injection tests, for some frontier testing approaches.
Frontend testing is a dangerous sophisticated world, which I know little about.
More links