tags:

views:

129

answers:

5

I use JUnit and the standard practice of having a XXXTest class for each class I am testing. When writing some tests today I noticed that the test class was about to reach 10000 lines.

Are there some best practices relating to the maximum length of a unit test class? Should I split my unit test class into multiple classes?

+3  A: 

Your unit tests should be as verbose as they need to be to reach the level of confidence you need in your code.

I'd say if your test type is that long it is likely because you have a lot of setup/teardown boilerplate, which could indicate you need to abstract some collaborators (e.g. use interfaces and mocking), or introduce some test helper methods to refactor the boilerplate. It might also indicate your type is doing too much, and needs refactoring.

If you refactor the type, you'll likely see the corresponding test type get smaller too.

Rich Seller
A: 

I would certainly split that up. Although there's no particular limit, I start to watch for abstractions/decompositions if my classes exceed a few hundred lines.

Perhaps this is an indicator that your class under test is itself too big, and/or perhaps doing too many things ? Can you break that class apart into different well-defined and self-contained components ?

Brian Agnew
The class under test is large - around 1500 lines. I have thought about refactoring but it's not straightforward given the task the class is performing.
Mark
That sounds like a large class. You should probably ask yourself whether the class is performing one clearly defined task, and (if not) whether it needs to provide a facade upon other classes to perform this
Brian Agnew
+3  A: 

It depends on whether this is actually a hint that the Class Under Test (CUT) is doing too much. If that is so then the obvious answer is to refactor the CUT which would mean dividing up the test class accordingly. Otherwise, the number of methods may not matter if the number of different scenarios you want to cover is that large.

10K lines is a bit much so the next question would be whether you can refactor the test to create private helper methods to clean out any repeated code, use Object Mothers or Mocking to keep down on duplication, etc. Either that or you're testing scenarios that are the same, like testing sum() with 1+1 and 2+2 and 3+3 where they aren't edge cases.

Kelly French
There are a large number of scenarios to test in this case. The test class has 40+ test methods.
Mark
Normally the convention is one test class for one application class but there is no rule against splitting the tests into separate test classes. Some might say it's a smell but it'll still be your call.
Kelly French
A: 

It sounds like you have to large of a class which you are trying to test. Your unit test should be focusing on testing a single class and if that class has so much functionality that it needs a 10000 line unit test it needs to be split up. Generally if a class is approaching 200 lines you need to refactor it into several classes.

A: 

Here are some more thoughts on reorganising large unit test classes that I came across today.

Mark