Like software, tests can be good or bad, with a whole range of shades in the middle. To write good tests, here are some guidelines:
- Keep them as simple as possible. It's okay to violate some good coding rules, such as hardcoding values or duplicating code. Tests need, first and foremost, to be as readable as possible and easy to understand. When tests are hard to read or understand, you can never be confident they are actually making sure your code is performing correctly.
- Tests should verify one thing and one thing only. It's very important that you keep them short and contained. It's perfectly fine to write multiple tests to exercise a single object or function. Just make sure that each test has one and only one purpose. ...