Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How deep are your unit tests?

The thing I've found about TDD is that its takes time to get your tests set up and being naturally lazy I always want to write as little code as possible. The first thing I seem do is test my constructor has set all the properties but is this overkill?

My question is to what level of granularity do you write you unit tests at?

..and is there a case of testing too much?

like image 643
Johnno Nolan Avatar asked Sep 30 '08 14:09

Johnno Nolan


People also ask

How thorough should unit tests be?

You're tests of the Wrapper class should be to make sure that it doesn't modify the data in any way, assuming that's its purpose of course. If it's purpose it to modify the data then you should test that. Show activity on this post.

Is there such a thing as too many unit tests?

Yes, it is possible to have too many unit tests - a simple thought experiment shows that you can continue adding tests that don't provide additional value, and that all those added tests can inhibit at least some refactorings.

How hard is unit testing?

But, being a white box software testing technique, unit testing is an in-depth process that requires a lot of knowledge about the code and how it interacts with other parts of the project. As a result, it can be hard to start writing unit tests, especially if you are new to the codebase.

What is acceptable unit test coverage?

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.


1 Answers

I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it. I do tend to make sense of test errors, so I'm extra careful when I have logic with complicated conditionals. When coding on a team, I modify my strategy to carefully test code that we, collectively, tend to get wrong.

Different people will have different testing strategies based on this philosophy, but that seems reasonable to me given the immature state of understanding of how tests can best fit into the inner loop of coding. Ten or twenty years from now we'll likely have a more universal theory of which tests to write, which tests not to write, and how to tell the difference. In the meantime, experimentation seems in order.

like image 61
Kent Beck Avatar answered Sep 20 '22 23:09

Kent Beck