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 2

In part 1 I was able to take an Angular controller written in JavaScript and convert it to a TypeScript file while doing very little to change the code. In this post I am going to explore transitioning that same controller to actually use the features provided in TypeScript. This is how I left off my controller:
declare var angular: any; (function () { 'use strict'; var controller: any = function($scope){ ... } angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; })();
While performing the translation from JavaScript to TypeScript, I would make sure at every step that the functionality I expected still worked, so if anything I did broke the system I would change it back and try again with another approach. Also if something seemed like it worked too easily, I would break it on purpose to make sure I wasn't getting a false result through browser caching a previously working fil…

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') …

Gamify TDD

I like it when things that would not normally be associated with games add concepts from games as a way to incentives you to accomplish things. Why simply go for a run if you can have an app that will track you and give you a gold star if you do better than you did the last time? Why go to the coffee shop that only gives you coffee if the other one will give you points that you can redeem for free drinks eventually?

I was recently introduced to CodeSchool, an online training system similar to PluralSight, it has video courses and challenges you can take to prove that you retained what the video taught. CodeSchool also adds badges and tracks to your learning, so as you complete a video and its challenges you get a badge. Complete a collection of courses within a specific discipline and you become a master of that discipline.

Some of these incentives are not tangible and really don't mean much in the real world, but they tend to work for me. If I start working towards a large goal a…