10 Good Testing Practices

10 Good Testing Practices

In modern software engineering testing is one of the main if not the main area which grantees application stability and quality. Without proper test coverage you can’t trust your code or prove that it is really working as required. So in this short post we will go though 10 basic good testing practices, every software developer should know by heart.

  1. Name your tests/test descriptions (depends on test framework) properly. Other people should understand intention of a test from its title. For example:
    • “should call users API if isClicked value is true” – GOOD.
    • “Calls API” – BAD.
    • callUsersApiIfIsClickedTrue – GOOD.
    • callsApi – BAD.
  2. All tests consists form 4 parts: Arrange, Act, Assert, Destroy. Destruction might be done automatically, but it is still there.
    • Arrange – test setup, object creation, mocks, etc.
    • Act – calls tested function/method.
    • Assert – validates test outcome.
    • Destroy – test cleanup, object/mocks destruction.
  3. Use exact/expected values instead of “any()” objects where possible. That helps to prepare for real user behaviour.
  4. Write negative test cases not only “Happy path” ones. People are different and they will use your software differently also negative cases helps you prepare for someone who will try to break your system on purpose.
  5. Extract magic strings (duplicated string variables) to constants, same as in program it self by doing that you will increase test suit performance and will make your life easier in case you need to refactor it.
  6. Extract repetitive code into private methods. Gives you better readability of test code and it will be easier to update your tests when needed.
  7. All tests needs to be properly asserted or verified otherwise we are creating fake test coverage. Which is worse than no tests at all, because you will think that test coverage is high, but then defects will start popping up on you Jira board.
  8. Do not mock the objects which aren’t used in a tests. This one is more about test performance than quality, but same as in your application code you shouldn’t have thing you don’t use.
  9. Throw Exception from test only if it’s required. I know it sounds silly, but I saw too many tests which just throws top level exceptions with no real reason “just in case”.
  10. Do not copy paste test cases, you will miss something and your test will end up inefficient or broken.

Add Comment

Your email address will not be published. Required fields are marked *