单元测试类的最大长度?

时间:2021-02-07 22:19:45

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.

我使用JUnit和我正在测试的每个类都有一个XXXTest类的标准做法。今天写一些测试时,我注意到测试类即将达到10000行。

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?

是否有一些与单元测试类最大长度相关的最佳实践?我应该将我的单元测试类分成多个类吗?

4 个解决方案

#1


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.

如果您重构该类型,您可能会看到相应的测试类型也变得更小。

#2


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.

这取决于这是否实际上暗示了被测试类(CUT)做得太多。如果是这样,那么显而易见的答案是重构CUT,这意味着相应地划分测试类。否则,如果要覆盖的不同场景的数量很大,则方法的数量可能无关紧要。

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.

10K行有点多,所以接下来的问题是你是否可以重构测试来创建私有帮助方法来清除任何重复的代码,使用Object Mothers或Mocking来减少重复等等。或者你正在测试相同的场景,例如测试sum()与1 + 1和2 + 2和3 + 3,它们不是边缘情况。

#3


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 ?

也许这表明你的受测试的课程本身太大了,和/或可能做了太多的事情?你能否将这个类别拆分为不同的明确定义和独立组件?

#4


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.

听起来你需要考虑大量的课程。您的单元测试应该专注于测试单个类,如果该类具有如此多的功能,需要10000行单元测试,则需要将其拆分。通常,如果一个类接近200行,则需要将其重构为几个类。

#1


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.

如果您重构该类型,您可能会看到相应的测试类型也变得更小。

#2


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.

这取决于这是否实际上暗示了被测试类(CUT)做得太多。如果是这样,那么显而易见的答案是重构CUT,这意味着相应地划分测试类。否则,如果要覆盖的不同场景的数量很大,则方法的数量可能无关紧要。

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.

10K行有点多,所以接下来的问题是你是否可以重构测试来创建私有帮助方法来清除任何重复的代码,使用Object Mothers或Mocking来减少重复等等。或者你正在测试相同的场景,例如测试sum()与1 + 1和2 + 2和3 + 3,它们不是边缘情况。

#3


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 ?

也许这表明你的受测试的课程本身太大了,和/或可能做了太多的事情?你能否将这个类别拆分为不同的明确定义和独立组件?

#4


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.

听起来你需要考虑大量的课程。您的单元测试应该专注于测试单个类,如果该类具有如此多的功能,需要10000行单元测试,则需要将其拆分。通常,如果一个类接近200行,则需要将其重构为几个类。