News for hackers. Stuff that matters.
Re:How deep are your unit tests? by MojoJolo on Monday December 10, 2012 @03:13AM (#4898039)
I agree with the first answer.

I'm not a fan of unit testing. I'm just doing it because it's a requirement (and we still have no QA).

Why I'm not a fan? Because I'm doing my own tests. And probably it has a bias all over it. I know when it will have a successful run and I expect where it will fail. This is not the scenario that I wanted. (I know this is not a good testing scenario)

In my opinion, a programmer must write a good code. And leave the testing to the QA.

Re:How deep are your unit tests? by tikhonj on Monday December 10, 2012 @04:36AM (#4898267)
This is one of the reasons I really like property-based testing à la QuickCheck[1]. The core idea is simple: you come up with an invariant for your code and the testing framework checks this invariant with randomly generated inputs. So when you're writing a test, you just have to come up with interesting invariants; you do not have to guess which inputs are edge cases.

[1]: http://www.haskell.org/haskellwiki/Introduction_to_QuickChec...

This may sound complicated and seem like overkill, but I've actually found it easier to use this style of tests for much of my code. Having randomly generated inputs can help find edge cases I did not even consider when writing the code in the first place--this addresses the bias issue you are worried about.

I also find that this style of test results in concise, easy to read tests. The usual "hello world" example uses the reverse function for lists. We want to ensure that for all lists, reversing it twice does nothing. The test would look like this:

    prop_reverseTwice ls = reverse (reverse ls) == ls
It's very easy to tell what invariant you're testing for! (The prop_* name is a convention for this sort of test.)

While real invariants are often more complex, I find they are still usually easy to read right from the code for the test.

Anyhow: you should really try QuickCheck or something like it for your language of choice.

Re:How deep are your unit tests? by danieldk on Monday December 10, 2012 @05:04AM (#4898337)
I agree that QuickCheck is really nice. It helped me spot some errors in e.g. tree diff'ing code that I wrote. QuickCheck especially works well for fundamental data structures and algorithms, where invariants are clear and simple.

However, in other cases, the usage is less obvious. For instance, suppose that you are writing, say a URL parser. What do you want QuickCheck to generate and check? If you generate too broadly, the invariant check becomes a parser itself, if you generate too specifically you might introduce bias again.

Re:How deep are your unit tests? by tikhonj on Monday December 10, 2012 @05:26AM (#4898401)
That's a good point. I remember having exactly that problem when writing tests for my toy language's parser. Generating random valid programs was actually tricky for several different reasons. In fact, I ultimately gave up on QuickCheck completely for that project, but mostly out of laziness.

Amusingly, I also wrote some tree diff code in Haskell recently. However, I did it rather poorly so I'm going to have to rewrite it soon (probably some time during winter break). I definitely agree that algorithms like this are particularly well-suited for QuickCheck.

Re:How deep are your unit tests? by selectnull on Monday December 10, 2012 @03:39AM (#4898109)
In my opinion, a programmer must write a good code.

I always write good code, but somehow that code turns to shit after six months, and I promise no one touched it. So, I guess I don't need the tests when I'm writing the code but I need them badly when I maintain it.

Re:How deep are your unit tests? by Ygg2 on Monday December 10, 2012 @04:47AM (#4898298)
That's quite opposite for me, I write code with lots of tiny errors (most of them 0,1 based array sizes or not understanding how API works), so I test as much as possible. But tests true value is regression testing. I can refactor something, and test knowing that if I messed something up, test will flare up.

I still try to avoid errors, but it's pretty much nigh impossible.

Re:How deep are your unit tests? by Vivtek on Monday December 10, 2012 @04:07AM (#4898184)
Oh dear Lord, how true this is.
Re:How deep are your unit tests? by philbo on Monday December 10, 2012 @04:02AM (#4898173)
I suspect there are some subtleties to that first answer which you've overlooked.

It is written by Kent Beck. He pretty much invented the concept of test-driven development. He's probably the most well-known advocate of developers writing their own tests there is.

What his answer demonstrates (to me) is that the conclusions he and the other XP guys arrived at - to write code in tiny chunks, each one preceded by a tiny failing test - is based entirely in pragmatism. He does it because it works better than anything else he's come across, including leaving the tests up to a dedicated QA team.

Re:How deep are your unit tests? by dalke on Monday December 10, 2012 @06:46AM (#4898586)
"... because it works better..."

Except that it hasn't been shown to work better. Quoting from http://third-bit.com/blog/archives/4529.html: "If you ask its advocates for evidence, they’ll tell you why it has to be true; if you press them, you’ll be given anecdotes, and if you press harder, people will be either puzzled or hostile."

There is very little research which shows that TDD is better than other approaches. Perhaps a bit, for some cases, but not enough to be certain about its general applicability. (Or has this changed in the last three years since I researched this topic?)

I hate writing code in "tiny chunks, each one preceded by a tiny failing test." I prefer to use a small set of burn tests until I have the main structure in place. Only then do I do the tiny tests - through the high-level API where possible (so I can refactor later) - and I use coverage analysis to make sure I'm not missing any obvious tests.

Coverage analysis is not part of TDD, which is a shame. (Technically that's because TDD is a design methodology, while coverage analysis is part of testing. While TDD generates tests, these are not a complete set of tests. For example, security analysis and scalability are outside the scope of TDD.)

Re:How deep are your unit tests? by stonemetal on Monday December 10, 2012 @07:27AM (#4898714)
Except that it hasn't been shown to work better. Quoting from http://third-bit.com/blog/archives/4529.html: "If you ask its advocates for evidence, they’ll tell you why it has to be true; if you press them, you’ll be given anecdotes, and if you press harder, people will be either puzzled or hostile."

Oh good, evidence against is just as preachy and unscientific as the evidence for.

http://evidencebasedse.com/?q=taxonomy/term/2

Research suggests that unit testing has a very strong correlation with correct software. Research suggests TDD has no correlation with correct software, except for the fact that TDDers tend to write 2x more tests than non TDDers.

Re:How deep are your unit tests? by pekk on Monday December 10, 2012 @08:37AM (#4898882)
Your requirement of 'evidence against' isn't how science works. The burden of evidence rests with the people making the claim. If you make a claim and do not provide evidence on request, that claim is not supported; it is not necessary to supply 'evidence against' for the claim to be unsupported, and there's no good reason to believe unsupported claims.
Re:How deep are your unit tests? by stonemetal on Tuesday December 11, 2012 @08:41AM (#4904443)
This is a discourse not science class both sides of the argument should have evidence.
Re:How deep are your unit tests? by dalke on Tuesday December 11, 2012 @10:03AM (#4904740)
Who says there are only two sides? There are dozens or hundreds. I can make a new one - programming is best done with a candle burning on the table and a glass of wine or whisky available. It works for me, so I say it should work for everyone. I call it "Romantic Driven Programming" or "RDP." Now, what's the evidence that you have against RDP as a best practice?

You don't have any, do you? So obviously I can now promote it as the new universal methodology.

(Or more directly, you think the argument is that there is evidence for and evidence against TDD. My argument is that there isn't enough evidence to make a conclusion.)

Given that this is a discourse, have you read any of the discussions of the topic of TDD? For example, "Making Software" (e.g Oram and Wilson) has an excellent chapter summarizing the research results. If not, then it's a rather unbalanced discourse, no? You pointed to a list of papers, but didn't describe what you wanted to discuss from them.

For example, some of the research compares test-first with test-last. I'm a proponent of test-during, and don't think that test-first and test-last are the only two ways to compare things.

Re:How deep are your unit tests? by jiggy2011 on Monday December 10, 2012 @08:40AM (#4898894)
I'm not sure how one would do such research.

You would need to compare teams of equal skill level who were working on very similar software over quite a long period of time.

What would the benchmarks be? Number of defects in production code? Time to ship? Severity of defects? Time spent debugging?

Re:How deep are your unit tests? by dalke on Tuesday December 11, 2012 @10:19AM (#4904798)
It's not easy research, but there have been some papers published on the topic. The problem of course is that there are a lot of confounding factors.

Besides, that my point, isn't it? Promotors of TDD argue that their personal intuition suffices, despite having little evidence to back their claims.

Re:How deep are your unit tests? by Firehed on Monday December 10, 2012 @04:56AM (#4898315)
> In my opinion, a programmer must write a good code. And leave the testing to the QA.

Aside from the "not my problem" vibe that gives off, I strongly disagree. QA is helpful for covering weird user interactions and writing tests against bugs discovered in production, but you as the programmer should know what your code is supposed to do while you're writing it. Best write tests then rather than six months later when you've forgotten what it was for.

Re:How deep are your unit tests? by AndrewDucker on Monday December 10, 2012 @03:50AM (#4898139)
And when someone else is maintaining your code, and changes something deep within in it that has an unexpected side effect, how will you tell whether it's broken something or not?
Re:How deep are your unit tests? by rjd on Monday December 10, 2012 @04:21AM (#4898220)
That is the only time I write unit tests. I never write them first run of software, but when I start changing the product down the track maintenance/upgrades/bugs/what ever I'll add tests to make sure things don't explode, and hopefully I'm not introducing problems. That said often things are rushed and they get ignored, even when I'm low on confidence about said introductions of errors :P