简体   繁体   English

如何组织复杂类的单元测试?

[英]How to organize unit tests for a complex class?

I have a complex class (300+ lines), which I'm trying to test from different "points of view". 我有一个复杂的类(300多行),我试图从不同的“观点”进行测试。 I've already created three different unit tests. 我已经创建了三个不同的单元测试。 Every test is a complex class itself (100+ lines). 每个测试本身都是一个复杂的类(100多行)。 The question is -- what is the best place to store them, in project directory tree? 问题是 - 在项目目录树中存储它们的最佳位置是什么? This is how I'm doing it now (Maven is used): 这就是我现在正在做的事情(使用Maven):

pom.xml
/src
  /main
    /java
      /com
        /Foo
          ComplexClass.java
  /test
    /java
      /com
        /Foo
          /ComplexClass
            FirstPointOfViewTest.java
            SecondPointOfViewTest.java
            ThirdPointOfViewTest.java

Of course, the names are just placeholders, used in order to explain the problem/question. 当然,名称只是占位符,用于解释问题/问题。 What do you think about this approach? 您如何看待这种方法?

Your class is so complex that you need three different test classes to test all the aspect of the class? 你的课程太复杂了,你需要三个不同的测试类来测试课程的所有方面? Probably you have mixed too many concerns in a single class. 可能你在一个班级中混合了太多的顾虑。 I would suggest refactoring the class using proven design patterns to separate classes with orthogonal concerns that can then be tested individually. 我建议使用经验证的设计模式对类进行重构,以便将具有正交关注点的类分开,然后可以单独进行测试。

One thing you might want to consider: if you keep your test code package structure the same as your main code package structure---even using different physical directories as you are currently doing---your test classes will be in the same logical package as your main classes. 您可能需要考虑的一件事是:如果您保持测试代码包结构与主代码包结构相同 - 即使使用当前正在执行的不同物理目录 - 您的测试类将位于相同的逻辑包中作为你的主要课程。 This means they gain access to default/protected members in the tested classes, which is often helpful. 这意味着他们可以访问测试类中的默认/受保护成员,这通常很有帮助。 You'd have to get rid of the ComplexClass package in the test code to make that happen. 您必须在测试代码中删除ComplexClass包才能实现这一点。

Another thing to consider: (I'll assuming you're testing with JUnit) Test classes are classes , so you can organize and structure them using inheritance. 另一件需要考虑的事情是:(我假设您正在使用JUnit进行测试)测试类是 ,因此您可以使用继承来组织和构造它们。 If you have 3 different points of view, maybe extract a base class which contains common functionality, this will make your tests easier to maintain in the long run, especially as more "points of view" are discovered. 如果您有3个不同的观点,可能会提取包含常用功能的基类,这将使您的测试从长远来看更容易维护,尤其是在发现更多“观点”时。

Separating the source and test code as you are already doing is a great idea, it gives you more options for building and maintains a logical grouping which makes maintenance more straightforward. 正如您所做的那样分离源代码和测试代码是一个好主意,它为您提供了更多选项来构建和维护逻辑分组,从而使维护更加简单。

I'd keep what you currently have. 我会保留你现在拥有的东西。 The main advantage of this maven structure is that rather than mixing source and test code together and trying to identify which classes to exclude from your build you just ignore the test directory entirely. 这种maven结构的主要优点是,不是将源代码和测试代码混合在一起,而是试图确定要从构建中排除哪些类,而只是完全忽略test目录。 The point of using the same package is to expose protected methods/variables to your test classes but not to a public API. 使用相同包的目的是将受保护的方法/变量暴露给您的测试类,但不暴露给公共API。

One thing I might suggest is something I picked up at a talk by John Smart on Test Driven Development which is to name your test classes in groups of functionality they are testing, so you just have FirstPointOfView.java which is testing the behaviour of your first point of view of the com.foo package. 我可能建议的一件事是我在John Smart关于测试驱动开发的演讲中提到的一个问题,就是将测试类命名为他们正在测试的功能组,所以你只需要测试第一个行为的FirstPointOfView.java com.foo包的观点。 This approach should make it more obvious when you can split a test class into individual classes, if they are actually testing different sets of behaviour. 如果将测试类拆分为单独的类,如果它们实际测试的是不同的行为集,则此方法应该更加明显。

Edit: if ComplexClass is a directory you should drop that, so that your tests are in the same package, I think I may have missread your example tree 编辑:如果ComplexClass是一个目录,你应该删除它,以便你的测试在同一个包中,我想我可能会错过你的示例树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM