views:

288

answers:

12

Possible Duplicate:
How deep are your unit tests?

Ok, so it's common wisdom that it's impractical (and maybe not even preferable) to get 100% test coverage. In my experience, there's some code that's simply more trouble to test than it is practical to do so. I've kind of developed an intuition about this. But my team is sort of new to the whole test first way of thinking. I'm having trouble thinking of any guidelines on this subject.

I'll take that as a prompt to mean that maybe my intuition is wrong. So I'll pose the question to you guys: what code shouldn't be unit tested? Bear in mind that we're in legacy code land (at least in Michael Feathers's definition), and I'm trying to push a "test existing code as you go" approach.

+1  A: 

Typically I don't unit test interactions with unmanaged code and almost never anything using HTTPRequest.

William Edmondson
I'm guessing you mean .Net's HTTPRequest?
Jason Baker
Heh...yes. Pardon my myopia. Sometimes I forget people use languages other than C#.
William Edmondson
+2  A: 

Getters and Setters

Anything that simply wraps a libary call and should throw an exception - yes I know we should test these but trying to create a broken network, or crashed DB server is just too hard IMHO.

MrTelly
I don't agree, they are easy to test.
Mercer Traieste
but it is so simple to test a getter/setter pair you may as well. If I had a penny for every time I've seen copypasta that is setting the wrong property... I'd have about 50p
Rich Seller
Why shouldn't getters and setters be tested?
Matt Howells
Getters/setters should be tested. Who's to say someone doesn't change one (incorrectly) in the future. The tests are there not just to test what you've written, but to ensure that changes don't go un-noticed.
Brian Agnew
Simulating a broken network is easy: just unplug your network cable.
MusiGenesis
Getters and Setters should not be tested because its a waste of time. It's tedious and boring. The very thing that motivates us to program in the first place is to get rid of the tedious and boring.
Mike Caron
Test anything that you don't understand at first glance.
Mike Caron
+9  A: 

The code I write most that I don't unit test is, in fact, the unit tests.

I think everything else should be testable, and I make an effort to architect it such that I can. The difficulties come with code relying on remote servers etc., and I try to mock as much of that as possible.

Brian Agnew
Unit tests are tested - the tests test the code, the code tests the tests.
Matt Howells
If you do fail-first, then your tests are tested. (Not continuously, I'll admit, but it's something.)
Joe White
It's not a closed loop. It's quite possible that the unit tests don't check (say) every aspect of an object returned by a method under test.
Brian Agnew
Or you could say that unit tests are tested by your code... I believe this to be a two-way relationship if its done right...
Arjan Einbu
Why are your tests so complicated that they need to be tested themselves?
Mike Caron
It's not a complexity issue. It's a completeness issue. If you get a large object back from a test method, do you check all the attributes ? If you do, that's potentially a lot of work. If you don't then you're not checking everything. You could check against a 'test' object, but if there are lots of changes in that object during development, then that's a pain to maintain. So you have to make a pragmatic decision as to what to test (hence impacting on the completeness of your tests)
Brian Agnew
I'll give you another example (something I've seen in real life). assertEquals(ref1, ref1); so someone has incorrectly written a test to check that ref1 == ref1 when they should be checking ref2 == ref1. Regardless of how they should have written the test/code, that unit test now obviously isn't doing what it should do. Test above is about as simple a est as you can get (and is wrong)
Brian Agnew
How do you test CSS?
01
@01 - I don't tend to do presentation stuff. When I have (charting etc.) I've rendered offline, generated an image and MD5ed it, and compared vs. a reference that I prepared during development. That ensures that particular charts (for stocks say) don't change when I change other charts (for bonds, say). Yes - it's a pain to maintain during development. I don't know for CSS in particular
Brian Agnew
+2  A: 

All code should be tested, but I agree its not always practical to automate those tests. You can, however, get pretty damn close to 100% automated test coverage if you design for testability, which test-driven development forces you to do.

Of course, 100% test coverage is not actually all that meaningful, as you might have tested that 100% of the branches in your code are exercised but you sure as hell haven't tested all the permutations of call order and data ranges.

GUIs are really hard to test - you might be able to confirm that some text appears on the screen when button A is clicked, but would your automated test notice that that text is bold when it should have been italic, or that it appeared as white on white?

Matt Howells
You can use framework like Selenium for it(if you do normal web app) and test it with javascript/jquery
01
Indeed, but its a lot of work to automatically test that the page renders exactly as you want it to. And what about a windowed GUI?
Matt Howells
A: 

The tests themselves shouldn't have to be tested, except when for example the verification code becomes unwieldy. This could happen especially when developing tests for code which is not well fit for testing.

l0b0
A: 

Code that needs a service up and running, and 500 lines of code on a method, and not built on an interface is usually hard to test. You need to brake it apart, and extract dependences from it first.

Mercer Traieste
A: 

As a quick answer, I would say you should really only heavily test blocks of code which have a high chance of throwing unhandled exceptions due to input. So, a function that shows a message box or reverses a string or creates and displays a form does not need to be heavily tested, whereas a function that takes a set of latitudes and longitudes and calculates the distance of the shortest path that includes all the points needs to be heavily tested with a wide range of input values (including totally invalid input data).

Knowing which kinds of functions are likely to fail out in the world and which ones aren't is the real benefit of experience.

MusiGenesis
A: 

I don't take the time to test any framework code (.NET, jQuery, etc.), no matter how much I swear that there's a bug in the framework. The assumption is that the framework code has already been rigorously tested, which lets you focus on testing your own code. This assumes, of course, that you are not building and testing said framework yourself.

Tim S. Van Haren
A: 

Code that doesn't work? Thats the code that isn't tested right?

Spence
A: 

The things to focus on testing are the things with complicated logic. You wouldn't test a simple getter/setter because it's straight forward and it creates a lot of tedious programming which is frustrating. You want your developers to avoid doing tedious things, otherwise they get bored and do something else.

What you would test are those things that involve some kind of computation. Examples are things like formatting string data into a display format (like rendering HTML), computing values from input sources, object state changing methods (outside of simple getters).

Focus on the complicated things that take more than 10 seconds to write.

Mike Caron
A: 

We typically don't test GUI; instead, we try to extract all of the interesting logic into a presenter, and then test that.

However, we do have some tests for our GUI units. These generally fall into two categories: (1) ancient code where the logic is tightly bound to the UI code, and we haven't taken the time (and risk) to disentangle them yet; and (2) times when we actually care about behavior that's GUI-only, like "make sure the third-party grid drops down the combo box automatically when you tab into a new cell".

Joe White
A: 

I'd not test getters and setters in general (as far as they really only set/get a single field, and not have another logic attached).

Apart from that, I think it's hard to set up general guidelines.

Beginning with test-first, you always think "what a stupid amount of work and thinking about how I can test something before actually writing it" - and later, you're really happy that you did it because testing thta piece of code manually would have been ugly, and you could correct some wrong ideas about how to implement it even before doing so - because you realized it already when thinking about your test.

That said, yes, trying to attach tests to legacy code can be a real hard work, because ot's not laid out to be tested.

Still, as long as it's not tested, it cannot be easily changed and enhanced - it's basically unmanageable as soon as it's orginal author leaves the house (taking the docs in his mind with him).

So, for newbies, I'd say, everything bu getters and setters - until you're familiar with it and devlop the intuition to see what's really not needed to be tested (which still should be not really much!).

Henning