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.
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
Post a Comment