5 ms·
Write them for everything (I'll note some exceptions). Use code coverage tools to see what parts of your code are not being hit by tests. You'll find some surpr
by imauld 9y ago
Write them for everything (I'll note some exceptions). Use code coverage tools to see what parts of your code are not being hit by tests. You'll find some surprises there (tests not actually testing what they say they do). When something breaks write a test that would have found the bug, and then fix it. A good test suite will save you tons of time. If a piece of code is hard to test, it's bad code (lot's of branching logic, doing way too much in oe function/method, managing/changing too much state). Re-write it so it's easy to test.
However, don't test the compiler/interpreter and don't test your framework/libraries. Tests shouldn't look like this:
def test_foo():
foo = Foo(a=1, b=2, c=3)
assert foo.a == 1
assert foo.b == 2
assert foo.c == 3
This test will never fail and won't tell you anything. Don't test your dependencies code if you're using active, popular packages. Test that your code passes in the right values, and handles any errors that could happen in a reasonable manner.
The time savings you can get out of a good test suite are almost limitless. Good test save lots of time in development and prevent broken things from making it to production.