Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum length for unit test classes?

Tags:

unit-testing

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?

like image 253
Mark Avatar asked Jul 15 '09 14:07

Mark


1 Answers

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.

like image 130
Rich Seller Avatar answered Sep 18 '22 21:09

Rich Seller