I'm sure most of you are writing lots of automated tests and that you also have run into some common pitfalls when unit testing. My question is do you follow any rules of conduct for writing tests in order to avoid problems in the future? To be more specific: What are the properties of good unit tests or how do you write your tests?Language agnostic suggestions are encouraged.
An ideal unit test would test every program path but it is unlikely that you can do that in a program of any complexity. A broad statistical sample will suffice if you cannot run all cases. You need to test a wide range of valid and invalid cases, focusing on borderline or limit cases, like handling divide by zero cases, negative numbers in square roots and IO errors. You have performance tests too to check whether it can handle heavy volume. Write test cases that are independent of each other. Use data as close to production level as possible. Try to use a test environment as similar as possible to production as well. Here is an article from ACM on this subject: https://ubiquity.acm.org/article.cfm?id=358976 There are entire books available on software testing and good articles in technical journals from ACM and IEEE Computer Society.