Live data from Hacker News

How deep are your unit tests?

stackoverflow.com

11–20 of 69 posts

Re: How deep are your unit tests?

#11
A good strategy I've found was suggested by Reid Burke in response to this answer:

http://reidburke.com/2012/09/27/write-code-that-works/

Your code isn't uniform, and so you shouldn't be expecting 100% code coverage on everything either.

Instead, assign to particular sections of your code levels of stability, and for the code that has the highest stability/least amount of expected future change, prioritize writing the most unit tests for those.

Re: How deep are your unit tests?

#12
Several successful projects exist without TDD (or even unit tests for that matter)

The whole of GNU+Linux for example (or at least most of it)

On the other hand I've seen several projects claim "very good TDD coverage" and then crash and burned when put into production (usually posted to HN)

Real world, real usage testing is essential. TDD is good for keeping you on track and avoid regressions

It's also good for complicated (small) pieces of software that do something complex and is prone to having its behaviour adjusted with time (think: reports, data consolidation or analysis, calculations, etc)

Re: How deep are your unit tests?

#13
The thing I really dislike are unit testing styles that use mock objects a lot.

I see some people say that you should test each object in isolation, mocking out any dependencies. That seems wrong headed to me.

I prefer my unit tests to test everything all the way down.

The only thing I would mock is the file system, which is useful for testing file loading code.

Re: How deep are your unit tests?

#14
post #2

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 wr…

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?

#15

The thing I really dislike are unit testing styles that use mock objects a lot. I see some people say that you should test each object in isolation, mocking out any dependencies. That seems wrong headed to me. I prefer my unit tests to test everything all the way down. The only thing I would mock is the file system, which is useful for testing file loading code.

I think that's more towards some kind of integration testing. You're supposed to test one unit independently, and then write integration tests that actually tests that the whole system works. Not saying that's the best way but i think that's how people think.

Re: How deep are your unit tests?

#16

The thing I really dislike are unit testing styles that use mock objects a lot. I see some people say that you should test each object in isolation, mocking out any dependencies. That seems wrong headed to me. I prefer my unit tests to test everything all the way down. The only thing I would mock is the file system, which is useful for testing file loading code.

I prefer my unit tests to test everything all the way down.

They already have that, that's called integration testing. Unit testing is trying to test as small of a unit of code as possible - this helps you identify exactly where the error is. If all of your tests are integration tests and something fails, you have no idea which part of your stack the problem is in.

Obviously different projects require different kinds and amounts of test cases, but I prefer a mixture of both unit tests and integration tests (unit tests for small, complex blocks of logic, integration tests for most of the other stuff).

Re: How deep are your unit tests?

#17
I find that simply assert'ing all non-trivial assumptions and invariants in the code is the form of unit testing that works the best in heck of a lot of cases.

If there's need for explicit tests, just use the code in the right way and see it exit cleanly. Then use the code in the wrong way and see it assert (first replacing the assert() with a throw/longjmp to catch failures without aborting).

Asserts also double as a concise context documentation. When an assert is triggered, it's typically easy to see what its condition means and what has gone wrong. So it's a win-win all around :)

Re: How deep are your unit tests?

#18

The thing I really dislike are unit testing styles that use mock objects a lot. I see some people say that you should test each object in isolation, mocking out any dependencies. That seems wrong headed to me. I prefer my unit tests to test everything all the way down. The only thing I would mock is the file system, which is useful for testing file loading code.

So, you prefer your unit tests to be integration tests?

Re: How deep are your unit tests?

#19

The thing I really dislike are unit testing styles that use mock objects a lot. I see some people say that you should test each object in isolation, mocking out any dependencies. That seems wrong headed to me. I prefer my unit tests to test everything all the way down. The only thing I would mock is the file system, which is useful for testing file loading code.

That's what I thought until my first summer internship at a Rails shop. Our unit test suite could take a very long time to run (on my Macbook Air dev machine, it would take 15-20 minutes to run). The slowdown came from testing ActiveRecord models with many dependencies, and that we were creating sample models. As a result, our tests hit the test database multiple times, which made them slow down.

Our solution was to start mocking as many models as we could, which really did improve test runtime.

(Slight edit: after reading the other replies, now I really understand what integration tests are for.)

Re: How deep are your unit tests?

#20
post #2

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 wr…

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.

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.

Post reply on HN