These tips refer to testing javascript using Mocha but other languages and testing frameworks are similar.
The key point is to make it as easy as possible to add tests, both initially and when a bug is found and you want to make sure it never regresses. When it's trivially easy to do, there's no excuse not to test.
Create a general way to add test cases rather than ad hoc tests. I do this by making my tests get their input from an array of objects (which contain all needed arguments and the expected result).
Name the test using the object being tested to create consistent and informative names for each test.
Adding a new test is simple - just add an element to the array with the specified input(s) and expected result.
Example: testing a function cleanStr(s) which
a) latinizes characters with diacriticals, e.g. á to a
b) lowercases all characters
c) replaces unacceptable chars with a space, and
d) consolidates all groups of spaces into one space
suite('Testing cleanStr', function() {
var cleanStrArray = [
{str:'', result: ''},
{str:'a', result: 'a'},
{str:'a3', result: 'a '},
{str:'3a', result: ' a'},
{str:'3a2', result: ' a '},
{str:'32a', result: ' a'},
{str:'3a2 ', result: ' a '},
{str:'a b', result: 'a b'},
// non-Latin chars
{str:'Á', result: 'a'},
{str:'á', result: 'a'},
{str:'à', result: 'a'},
{str:'ñ', result: 'n'},
{str:'Él está en el baño', result: 'el esta en el bano'},
];
// create a name for each test in the array
cleanStrArray.forEach(function(aTest) {
if (!aTest.testName) aTest.testName = '"' + aTest.str + '" -> "' + aTest.result + '"';
});
// run the tests
cleanStrArray.forEach(function(aTest) {
test(aTest.testName, function() {
const newstr = travesty.cleanStr(aTest.str);
expect(newstr).to.equal(aTest.result);
});
});
});