Skip to main content

The case for automated testing

For the purposes of this post, unit tests, will be analogous to any automated testing system. Whether unit tests, integration tests, or any other means of having computers run reliable tests to verify that code is doing what it is intended to do.

When I was first introduced to the idea of automated, unit style, testing, I got it right away. It was one of those, "of course, why didn't I think of that" moments. It seemed like such an obvious improvement to me that it has been hard for me to understand why everyone isn't doing it, and doing it all the time. Since I have learned about unit testing, I have focused my learning and growth toward understanding how and where to use unit tests and how to make my code work better with unit tests. In that time I have improved my code and myself as a developer. I have also heard some arguments as to why everyone isn't always developing software with unit tests.

For a large application that has been developed over many years without unit tests in place, it can be difficult to integrate unit tests, so one of the first problems with getting people to buy in to unit tests is finding a place to start. You are not going to be able to cover your entire application with tests on day so you shouldn't try to do that, but you also shouldn't sit back and wait for some day in the future when you can. I think the best place to begin is to when you find a bug. If you can recreate a bug, you have an opportunity to wrap that bug in a test and have the test fail. If you can run a unit test that fails the same way as the bug, while also adding a few tests that pass where they should, then you can fix the bug and see the failing test now passes, you immediately know that it has been fixed.

The key to starting out is to focus on very small sections of code that do not depend on other code. The smaller the segment of code you can wrap in a test the better. As your system has more tests in place and you need to work on larger areas of the application, you can work on creating larger test fixtures and covering more of the system.

The next issue I have run into with getting unit tests going is that the majority of the code I have worked on has dependencies, and they are normally not passed along in a nice way, they are usually called deep in a function that you don't even realize is there unit you try to run a test that fails because Session doesn't exist. This is where more drastic changes will need to take place, so I would recommend trying to break these apart into small more manageable pieces as needed and pass in dependencies instead of calling them inside the function. Then you can place tests around those smaller chunks and move forward with the next part of your application that needs attention.

So now what happens when you have a ton of tests throughout your system? How can you possibly maintain all of those tests. What happens when a change breaks 100 or more tests? Well that is where you probably didn't design your tests very well. One of the key things to remember when writing tests is that you should not suddenly abandon good coding practices just because you are writing tests. If your application has 1 repository, your tests should probably also use 1 shared repository. If you are writing duplicated unit test, or if you are copy and pasting your tests to try out other variables, then you should look at consolidating that duplication into one area. Ideally you want your test project to be the best code in your entire application. If you follow best practices when writing your tests then you won't have a scenario where many tests fail due to any 1 change, your tests will cover very small, very specific pieces of code and having 1 break should not affect others.

Well that is all fine and good, but writing all of those tests will take time and resources. For every piece of code you write you are probably going to write more lines of test code. It is true that you will write more lines of code, but it is not a 1 to 1 time exchange. The act of typing code is not the time consuming piece of software development, thinking through the problem is. Which leads me to the biggest advantage to writing unit tests, saving time. Unit tests will save you and your company time, it will make you a more productive developer, and it will make your application more reliable, easier to understand, and easier to modify.

Think about a bug in a web application. You have to make the change in code, rebuild the solution, switch over to the browser, and recreate the action that causes the bug to happen. Did you fix the bug on the very first change? Probably not. Now rinse and repeat until you actually fix the bug. Depending on what set of actions it takes to start the bug off, this can really add up to a lot of time spent. If you instead created a unit test that can fail along with the bug, you still need to work through fixing the bug in code, but the steps to verify if your changes worked or not are reduced to run the unit test. Now that the bug is fixed you pass it along to QA and they find something else that you missed. You get to redo that process while checking 2 things to make sure your new changes to fix item #2 didn't mess up item #1, or you can write a new unit test and run both in virtually no time compared to testing it manually in the browser.

I can't imagine not using unit testing now based on how much I have improved and how much my code has improved since I started using it. I will continue to create unit tests wherever I can and the code I write will be better for it. I urge you to do the same. Don't wait until another day, don't be afraid that the system you work on is too large and cumbersome to be tested. Start out today and start out small. Grow from there, and you won't look back.

Comments

Popular posts from this blog

Converting a Large AngularJS Application to TypeScript Part 1

I work on a project that uses AngularJS heavily. Recently we wondered if using a preprocesser like CoffeeScript or TypeScript for our JavaScript would be beneficial. If our team is going to switch languages, we would need to be able to convert existing code over without much pain and we would have to find enough value in switching that it would be worth the conversion. I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript. This is what the controller JavaScript file looked like to start out with: ( function () { 'use strict' ; angular .module( 'app'

Interns: Taking off the training wheels

My intern team has been working for several weeks now on our new website. We have already completed one deployment to production and are finalizing our second one. We started with a plan to release often adding small bits of functionality as we go and so far that plan has been working really well. We already feel like we have accomplished a lot because we have completed many of our project's requirements and should easily be able to complete the rest giving us time to do even more than just the original requirements. One of the things I have had some difficulty balancing has been how much to lead the interns and how much to let them figure out on their own. In deciding what our team process should be and how we should allocate our time, I think it was important for me to do more leading. I saw some deficiencies in how we were currently working and brought up some ideas for how we could address them. We had moved into spending all our time just working through stories and did not

My idea for Hearthstone to add more deck slots

Recently someone asked the Blizzard developers for more slots for decks in the game Hearthstone. The response was that they are talking about it and looking into it, but no decision has been made yet. One of the concerns over adding deck slots is that it could complicate the UI for Hearthstone and make it more difficult for new players to understand. I have what I think would be a good solution to add more deck slots without increasing the learning curve for the game much if at all. First I would take a look at the current selection screen for starting to play a game. It defaults to showing the decks that are custom built by the player if they have any custom decks, and there is an option to page over to the basic decks. This basic deck screen is perfect for how I would change this process. Instead of having 2 pages of decks, 1 for basic and 1 for custom, you would just see the select a Hero screen. Then once you selected the Hero you wanted, you would see all of the decks that