Are you really using unit tests?

Unit TestingTesting

Unit Testing Problem Overview


I have been involved in a lot of projects, both old and new, and one thing that they have in common is that almost none of them have been using unit testing. I prefer to use it, but often the customer isn´t ready to pay for that, and suppose that the code just works as it should.

So, do you use unit testing in your projects, or do you rely on your coding skills?

Unit Testing Solutions


Solution 1 - Unit Testing

Using unit-testing is a coding skill.

I find it adds very little overhead to coding time. On top of that the code produced tends to be much easier to understand and to refactor, with an untold reduction in maintenance time.

A full discussion of the benefits here: Is Unit Testing worth the effort?

Solution 2 - Unit Testing

I'll be honest. I've only recently started writing unit tests, and when I go back to modify an old DLL that's from my bad old days, I'll hunker down and write unit tests to get it near 100% code coverage. I say "near" because the last few percent can be hard to get to due to the way that the code is structured, that it didn't have mocking or unit testing in mind (this happens a lot with the abstract classes or classes that P/Invoke into native code). And while I understand that 100% code coverage is not the same thing as 100% of all code execution paths, I've found that having the tests there is a good way to tell when I'm doing something that's going to break a lot of things.

To be honest, this is probably one reason why it has taken many developers (including myself) so long to get around to adopting unit testing (let's not get into TDD just yet).

I'm not saying that any of these are legitimate reasons, but they did more or less go through my head, and I bet they went through some of yours too:

  • First, there's a thought along the lines of "Oh, I've written mountains of code with zero tests, and seeing as I'm already crushed by that mountain, I don't possibly have the time to add several more mountains of test code on top of it."

  • Second, nobody likes to talk about the fact that the classic code-run-debug cycle actually works "good enough" when you're

  • writing new code that does not alter the behavior of old code

  • working on a relatively small software project or a throwaway utility

  • the sole developer on a project (you can keep most of it in your head, up to a point)

  • Third, unit tests are easier to appreciate when you're maintaining existing code, and if you're always writing new code, well, the benefits aren't immediately obvious. When your job is to churn out a utility and never touch it again, a developer can see little value in the unit test because they probably won't be working on that utility or at the company by the time a maintenance programmer (who wishes there was a unit test) comes around.

  • Fourth, unit testing is a fairly recent innovation. (Oh, hush ... I know the idea has been around forever, especially in mission-critical and DoD applications, but for the "Joe the Plumber Developer" types like me? Unit testing wasn't mentioned at all during my entire CS undergraduate career in the early part of this decade; in 2008, I hear it's a requirement for all projects from level 101 up. Visual Studio didn't even get a built-in testing framework until this year, and even then only in the professional edition.) Was it blissful ignorance on my part? Sure, and I think a lot of people who code because it's their day job (which is fine) simply aren't aware. If they are, then they know that they have to stay current, but when it comes to learning a new methodology (particularly one that involves writing more code and taking more up-front time when you needed that new feature done yesterday) means that it'll get pushed back. This is the long tail, and in a decade or so our talks about unit testing will seem as quaint as our mutterings about object-oriented programming enabling a new era of development during the 1990s. Heck, if I've started unit testing, the end must be near, because I'm usually an idiot.

  • Fifth, unit testing some areas of code is really difficult, and this scared me away for a while. Multi-threaded event queues, graphical rendering, and GUIs come to mind. Many developers realize this and think "well heck, unit testing would be great for library code but when was the last time I wrote just a class library." It takes a while to come around. To that end, no unit tests does not necessarily mean bad code lies ahead.

  • Finally, the level of evangelism that sometimes comes from unit testing advocates can be intimidating and off-putting. Seriously, I thought I was a moron because I just couldn't grasp how to mock a certain interface and why I would want to. Selfishly, I wanted to hate unit testing if just for the fact that everybody would not shut UP about it. No silver bullet.

So, apologies for the rambling post. In summary:

  • I did not unit test for a long time. Now I do. Unit tests are a great tool, especially during maintenance.

  • For existing projects, you can at least go in and write a test that documents a current input and its output. When you change that big mess of murky code in the future, you'll be able to see if you've altered that little snapshot. This is also an excellent way to change the development culture in your company.

Let the flames begin! =)

Solution 3 - Unit Testing

Unit testing can't be an afterthought, it is and has to be something which factors in to your design. I would go so far as to say that even if you never write or call a single unit test, the benefits it has in leading tight component driven software is worth the effort 95% of the time.

Solution 4 - Unit Testing

After coming onto a couple of projects that were in production but needed major new functionality, one of my bottom lines as a technical lead starting up a project is that unit-tests are a must.

It just costs too much to try and rewrite code that has been written without unit tests. The code is invariably poorly structured (A multi-thousand line web-service all in a single code behind anyone?) and making changes to it (even when it is well structured) without introducing bugs is a really painful process.

This becomes particularly true when a project enters fire-fighting mode (and not having unit tests contributes to projects getting into that state too) - customers are getting grumpy, they've lost faith in the project, few things worse than being the poor guy trying to get the next fix in without introducing a whole pile of regression bugs, and not even having unit tests.

Those situations can be so easily avoided or at least mitigated by explaining the value of tests up front. Of course there are situations where unit tests aren't so important but they are truly the exception.

So yes - I insist on unit tests and have spent a lot of time fixing the messes made by other developers who relied on their coding skills.

Solution 5 - Unit Testing

Writing and running unit-tests is part of a healthy coding process, it is not an addition the customer should have the choice to pay or not pay for.

Testing strategy is a coding issue just as any other: what datastructures to use, variable naming conventions, comment standards, etc.

Solution 6 - Unit Testing

I use unit testing, and tdd, whenever I can. However in my experience for every unit test advocate there are three or more developers who don't really believe writing unit tests is worth the effort, and that it slows down development. However these people tend to keep quiet, so you are unlikely to see many here.

Solution 7 - Unit Testing

I like Bob Martin's analogy: imagine you're a surgeon. What would you say to a patient who wanted to pay the surgery but told you to skip washing up ahead of time?

When a client hires me to code they are hiring me as a professional, as someone with the skills and discipline of a professional. My job is to give them "code just works as it should", and I know the best way for me to do that is to use TDD.

Solution 8 - Unit Testing

I often use unit testing for complex mathematical algorithms, for example for functions like LineIntersectsLine where you can define some important examples to test. After that, it is easier to control this function. You can simply rewrite/optimize it and test if it still works, or you can add new tests if you encounter bugs and then try to correct these.

Solution 9 - Unit Testing

I find newer developers benefit more from unit testing than older ones who had to learn from the school of hard knocks of the pitfalls that could make something fail. Unit testing does not lead to good design - it just leads to a design that is more testable. You need to test your code but the way unit testing is preached is dangerous. "It will force you to design your code better", "How can you test whether it is correct?".

I prefer to have well written structured code that when you read it automatically tells you simply what it is trying to accomplish and how it does it. Functions/classes should be small and concise. They should only have one responsibility. Unit tests don't protect against logical errors.

Unit tests give more false positives than anything else particularly when a project is first written. Good design trumps tests - tests should be the verification stage nothing more. I never bought into the testing comes before everything else concept. In my experience this line of thinking favours testability at the expense of extensible code (may be ok for throwaway projects or one off utilities but ironically unit testing isn't as important for these).

Solution 10 - Unit Testing

I agree with Ken that unit testing is part of software development.

About the cost question, it's true than writing the code plus the unit test is longer than writing just the code. However, when you write the code along with its tests - which is called TDD - Test-Driven Development, you end with "clean code that works". When you just write the code, then you have to make it work, which can be long and painful.

Another benefit is that you know where you are, as the code that has been written has already been unit-tested.

To answer your question, yes, I'm using unit testing on my projects when it's possible. I write unit tests for all the new code and I strive to for legacy code.

Solution 11 - Unit Testing

I'm a big fan of unit testing, mainly because it's completely addicitive - the "code-test-see horrible red bar-fix-test-see lovely green bar" cycle in JUnit seriously gets the endorphins pumping.

Solution 12 - Unit Testing

My company makes system components for various companies in the Aerospace Industry. The FAA requires Modified Condition/Decision Coverage for Quality level A Safety Critical flight software (DO-178-B) So for verification we do (again from DO-178-B):

Analysis of all code and traceability from tests and results to all requirements is typically required (depending on software level).

This process typically also involves:

>Requirements based test tools

>Code coverage analyser tools

>>Other names for tests performed in this process can be:

>>> Unit testing

>>> Integration testing

>>> Black box and acceptance testing

Unit testing reveals code errors all the time.

Solution 13 - Unit Testing

In regards to relying on my coding skills, I find that my coding skills actually improve when I use TDD and rely on my unit tests to correct me when I break a test. You learn how to use many language features because you can make a tweak to test out an assumption.

Solution 14 - Unit Testing

I know from experience that when I write code without unit tests, I'll get a few issues that come up to fix, but when I've written the unit tests I rarely hear of issues. Also, from time spent writing unit tests I write better code to begin with. I look at methods and think of the ways they could fail and build them from the start not to fail or at least to fail in a better way.

Unit tests ARE essential to writing better code, but it also will make you a better developer because you SEE areas where things could go wrong and fix them before you ever get to testing.

Solution 15 - Unit Testing

Unit testing is an essential part of development, and (I have found) will actually reduce the time to completion of a project while improving overall quality, especially when done in a TDD fasion.

Solution 16 - Unit Testing

I do Unit test. I am actully building currently a constraint validation engine and my customers want it bullet proof. Well, without unit test, I would die from stress...

So yes, I use it !

Solution 17 - Unit Testing

Most functions that I write have tests. Like many have said up there, unit testing is an essential part of software engineering. So, to answer your question, yes, I REALLY write and run tests.

Also, with continuous integration, regression tests will be automated and constantly reported.

Solution 18 - Unit Testing

Another very useful reminder of why you want to unit test wherever you can just showed up in this post: http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html">Top 12 Reasons to Write Unit Tests I need to have that engraved on my retinas.

I can testify personally to the value of thinking about how something will be tested from the beginning, with tests developed through each iteration. I need to be more systematic, myself. I am printing out that list right now.

Solution 19 - Unit Testing

Your customers would probably save money overall if unit testing was in place. Some of the errors prevented by unit testing are much more of a liability if found later in the development stage rather than during unit testing. It saves so much headache in the future, now that I use it I don't think I could ever go back.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikael SöderströmView Question on Stackoverflow
Solution 1 - Unit TestingKenView Answer on Stackoverflow
Solution 2 - Unit TestingNicholas PiaseckiView Answer on Stackoverflow
Solution 3 - Unit TestingannakataView Answer on Stackoverflow
Solution 4 - Unit TestingDavid HallView Answer on Stackoverflow
Solution 5 - Unit TestingJesperEView Answer on Stackoverflow
Solution 6 - Unit TestingDavid SykesView Answer on Stackoverflow
Solution 7 - Unit TestingJeffrey FredrickView Answer on Stackoverflow
Solution 8 - Unit TestingschnaaderView Answer on Stackoverflow
Solution 9 - Unit Testinguser1950070View Answer on Stackoverflow
Solution 10 - Unit TestingphilantView Answer on Stackoverflow
Solution 11 - Unit TestingnayfnuView Answer on Stackoverflow
Solution 12 - Unit TestingPaulView Answer on Stackoverflow
Solution 13 - Unit TestingcasademoraView Answer on Stackoverflow
Solution 14 - Unit TestingxandoView Answer on Stackoverflow
Solution 15 - Unit TestingCodebeefView Answer on Stackoverflow
Solution 16 - Unit Testinge-satisView Answer on Stackoverflow
Solution 17 - Unit TestingyclianView Answer on Stackoverflow
Solution 18 - Unit TestingorcmidView Answer on Stackoverflow
Solution 19 - Unit TestingRyan ThamesView Answer on Stackoverflow