4 ms·
Always write them when the code will actually be used. You typically test all "trivial" functions, partly because they're easiest to test and sometimes very ea
by mod 9y ago
Always write them when the code will actually be used.
You typically test all "trivial" functions, partly because they're easiest to test and sometimes very easy to screw up. It can be much harder to properly test non-trivial functions. Often you want to split them into smaller, testable units.
Functions that write to database get tested. The tests spin up a temporary test database and run against that. See rails for an example. Often just against sqlite3 or something, but of course that's configurable.
You can justify them because tests save time. They make starting slower (especially when you're new to testing) and they make editing and altering much, much faster. They also lead to more reliable applications on average, and the ability to make changes quickly.
I don't follow my own advice, because I'm lazy. But I really wish I tested every bit of code that I could. And it's the stupid kind of lazy--testing would make my life easier, all things considered.