views:

515

answers:

5

On the SO blog and podcast Joel and Jeff have been discussing the, often ignored, times when unit testing a particular feature simply isn't worth the effort. Those times when unit testing a simple feature is so complicated, unpredictable, or impractical that the cost of the test doesn't reflect the value of the feature. In Joel's case, the example called for a complicated image comparison to simply determine compression quality if they had decided to write the test.

What are some cases you've run into where this was the case? Common areas I can think of are GUIs, page layout, audio testing (testing to make sure an audible warning sounded, for example), etc.

I'm looking for horror stories and actual real-world examples, not guesses (like I just did). Bonus points if you, or whoever had to write said 'impossible' test, went ahead and wrote it anyways.

A: 

Once I wrote a unit test to expose a concurrency bug, in response to a challenge on C2 Wiki.

It turned out to be unreasonably hard, and hinted that guaranteeing correctness of concurrent code is better handled at a more fundamental level.

Morendil
Or maybe its handled with a different concurrency approach, such as using channels to pass state changes between threads, or ensuring all shared state is immutable. Massively parallel applications (10s of 1000s of clients accessing state concurrently and distributed computing) use this approach.
Juliet
+10  A: 
@Test
public void testSetName() {
    UnderTest u = new UnderTest();
    u.setName("Hans");
    assertEquals("Hans", u.getName());
}

Testing set/get methods is just stupid, you don't need that. If you're forced to do this, your architecture has some serious flaws.

furtelwart
Btw... this is the least "worth it" answer I've given on SO :D
furtelwart
Were you adding just a getter/setter, or were you adding code that relied on the getter/setter? Couldn't you test the latter?
Andrew Grimm
Damn, I forgot the <irony> tag... This unit test is just an example for several definitely useless unit tests.
furtelwart
In general, a unit test where the lines of code written to do the test will (a) never find a regression, or (b) find regressions that are not failures, but rather measurements that are analog (goodness and badness as percentage) or subjective (image quality, anyone?) rather than Pass/Fail booleans.
Warren P
+1  A: 

It sounds to me like the writing of a useless unit test is not the fault of unit tests, but of the programmer who decided to write the test. As mentioned in the podcast (I believe (or somewhere else)) if a unit test is obscenely hard to create then it's possible that the code could stand to be refactored, even if it currently "works".

Even the "stupid" unit tests are necessary sometimes, even in the case of "get/set Name". When dealing which clients with complicated business rules, some of the most straightforward properties can have ridiculous caveats attached, and you mind find that some incredibly basic functions might break.

Taking the time to write a complicated unit test means that you've taken the time to fine-tune your understanding of the code, and you might fix bugs in doing so, even if you never complete the unit test itself.

I agree, although needing to implement an image library to compare JPEG compressions is a bit much. Also, there's a subtle difference between a unit test that's complicated and a unit test that's 'too complicated'
Soviut
'too' complicated would be "more complicated than necessary" in which case, why didn't you stop at "as complicated as necessary" =D
To clarify again, I'm referring to 'too complicated' as any test that's not 'worth it' in terms of the value added by writing the test.
Soviut
+2  A: 

My company writes unit tests and integration tests seperately. If we write an Integration test for, say, a Data Access class, it gets fully tested.

They see Unit Tests as the same thing as an Integration test, except it can't go off-box (i.e. make calls to databases or webservices). Yet we also have Unit Tests as well as Integration Tests for the Data Access classes.

What good is a test against a data access class that can't connect to the data?

tsilb
+5  A: 
Foo foo = new Foo();
Assert.IsNotNull(foo);
Jim Arnold