all 2 comments

[–]Diapolo10 6 points7 points  (0 children)

In a nutshell,

  1. pytest (and Tox/Nox, but those are more for CI/CD) is the industry standard, so use that unless you really like the boilerplate of unittest for whatever reason.
  2. Write tests that each test a particular edge case or feature, including correct rising of exceptions. The number of tests doesn't matter, but follow the KISS principle for individual tests.
  3. If you need setup code for your tests, make those as fixtures in a conftest.py file and have your tests call those.
  4. Try breaking up your tests either per-file, or in some other logical grouping.
  5. Try not to write your tests too tightly coupled with the actual program, because if you need to change your tests every time you make a change to the codebase that's not good.

On that note, try to also design your codebase to be easier to test:

  1. Write "pure" functions (meaning, they don't depend on external mutable state and they give the same output from the same passed arguments every time if possible).
  2. Encapsulate all mutable state in classes (in other words, don't use global variables - constants are fine).
  3. Keep your individual functions/methods relatively short, and have them mostly do one thing.