Thursday, 8 January 2015

Treating Test Code as Production Code

It's important when writing automated tests to remember that the code you write should be up to production standards, meaning any conventions that you have in place should be adhered to and that it should follow good design patterns.

Too many people often say why does it have to be as good as production code, it's "Only" a test, so long as it passes then that's fine...

To answer this we need to look at why we want our tests to be written in such a structured and efficient manner:

- Maintainability - by making the test code structured and efficient, it becomes far easier to maintain and in doing so changes in the future can happen quickly as the test isn't linked to anything that it shouldn't be and it's easy to understand for a new set of eyes.
- Durability- Again by making the tests structured they should be resistant to changes, if you change a variable name for instance then it shouldn't effect the unit test unless it absolutely has to.
- Flaky - We've all been there, had tests flaky and failing when they really shouldn't be failing, you run it again and hey presto! it works!

And finally, and perhaps the most important:

- Defects - The main reason in writing automated tests is to identify defects earlier and often and quickly. If we ignore the above, then we'll end up ignoring tests or in some cases removing tests that are flaky, and if we're removing tests then the defects that the tests were put into find, could quite easily work their way into production which is not what we want.

I know maintaining tests is time consuming and people struggle to comprehend the value in keeping tests maintained and up to date, until of course bugs are found in production, then if only we had a time machine to go back and run the tests that were removed or commented out!

So this is why I believe that test code is just as important (and you could easily argue that it is more important) than the actual code that it tests.

Anyone agree/disagree????


2 comments:

  1. At this link you can find even more great writing tips and tutorials for bloggers.

    ReplyDelete
  2. Thanks for sharing helpful information, I really like your all post. I will bookmark your blog for future updates. Send Gift To pakistan

    ReplyDelete