Do you have tests for difficult to spot errors (e.g. arithmetic, rounding, regular expressions)?

Last updated by Lee Hawkins [SSW] almost 2 years ago.See history

By difficult to spot errors, we mean errors that do not give the user a prompt that an error has occurred. These types of errors are common around arithmetic, rounding and regular expressions, so they should have unit tests written around them.

Sample Code:

unit test   arithmetic code

Figure: Function to calculate a total for a list of items

For a function like this, it might be simple to spot errors when there are one or two items. But if you were to calculate the total for 50 items, then the task of spotting an error isn't so easy. This is why a unit test should be written so that you know when the function doesn't work correctly.

Sample Test: (Note: it doesn't need a failure case because it isn't a regular expression.)

unit test   arithmetic tests

Figure: Test calculates the total by checking something we know the result of.

We open source. Powered by GitHub