简体   繁体   English

我应该在哪里进行JUnit测试?

[英]Where should I put my JUnit tests?

I've got 2 questions about organising Unit tests. 我有2个关于组织单元测试的问题。

  1. Do I have to put test to the same package as tested class, or can I organise tests in different packages? 我是否必须将测试放在与测试类相同的包中,还是可以在不同的包中组织测试?

    For example if I have validity and other tests, is it correct to split them into different packages, even if they are for same class? 例如,如果我有有效性其他测试,将它们分成不同的包是否正确,即使它们是同一个类?

  2. What about mock and stub classes? 模拟和存根类怎么样? Shall I separate them from packages containing only tests, or put them together? 我应该将它们与仅包含测试的包分开,还是将它们放在一起?

The way we do our JUnit test cases is to put them in the same package, but in a different root directory. 我们执行JUnit测试用例的方法是将它们放在同一个包中,但是放在不同的根目录中。 Since we use Maven, we just use the standard locations making the structure similar to the following. 由于我们使用Maven,我们只使用标准位置,使结构类似于以下。

src/main/java/com/foo/Bar.java
src/test/java/com/foo/BarTest.java

Obviously there's more to the structure, but this lets us build the tests separately from the mainline code, but still access protected classes and the like. 显然,结构还有更多,但这可以让我们与主线代码分开构建测试,但仍然可以访问受保护的类等。 With respect to different types of tests, this is very subjective. 对于不同类型的测试,这是非常主观的。 When we started our testing effort (which unfortunately started after development), I tried to keep things pretty isolated. 当我们开始我们的测试工作(不幸的是在开发之后开始)时,我试图让事情相当孤立。 Unfortunately, it quickly became a nightmare when we got to the 500+ test case point. 不幸的是,当我们达到500+测试用例点时,它很快就变成了一场噩梦。 I've since tried to do more consolidation. 我已经尝试过更多的整合。 This led to reduced amounts of code to maintain. 这导致维护的代码量减少。 As I said, though, it's very subjective. 正如我所说,这是非常主观的。

As far as test-only code, we keep it in a separate com.foo.test package that resides only in the src/test/java tree. 至于只测试代码,我们将它保存在一个单独的com.foo.test包中,该包仅存在于src/test/java树中。

I too tend to put my tests in the same package but under a different root directory. 我也倾向于将我的测试放在同一个包中但在不同的根目录下。 This allows me to test package-private classes or access packing-private classes while testing something else in the package. 这允许我在测试包中的其他内容时测试包私有类或访问包装私有类。 They are kept in a separate directory tree to allow excluding them from the deployed result (in particular to ensure that test code didn't accidentally get into production code). 它们保存在单独的目录树中,以允许将它们从已部署的结果中排除(特别是为了确保测试代码不会意外地进入生产代码)。 What matters most, however, is what works for your situation. 然而,最重要的是适用于您的情况。

In terms of how many test classes per production class, the theory I've seen is that you write one test class per fixture, that is per setup structure. 就每个生产类的测试类数而言,我所看到的理论是,每个夹具编写一个测试类,即每个安装结构。 In many cases that is the same (or close enough) to one test class per production class, but I have sometimes written more test classes (in particular equality tests tend to be separated) for a give production class, and occasionally one test class of for a group of (related) production classes (say, for testing the Strategy pattern). 在许多情况下,每个生产类与一个测试类相同(或足够接近),但我有时为一个生产类编写了更多的测试类(特别是相等测试往往是分开的),偶尔也会有一个测试类对于一组(相关的)生产类(例如,用于测试策略模式)。

Mostly, I don't worry too much about the theory, but rework the tests as needed to keep duplication to an absolute minimum. 大多数情况下,我并不太担心这个理论,但是根据需要重新进行测试,以便将重复保持在最低限度。

Test classes should be rather in different packages, it's easier to separate them from the production code when you package it for release. 测试类应该在不同的包中,当您将其打包发布时,更容易将它们与生产代码分开。 I usually keep lots of test fluff in those packages, all sorts of mocks, configurations, scenarios.. But when you build - it doesn't get it. 我通常会在这些软件包,各种模拟,配置,场景中保留大量测试漏洞。但是当你构建时 - 它不会得到它。 In some situations, it's a good idea to keep your testing stuff even in different projects. 在某些情况下,将测试内容保存在不同的项目中是个好主意。 Depends. 要看。

Keeping it the same package allows you to use package-private visibility for code that is intended to be accessed via the test only. 将它保持为相同的包允许您对仅通过测试访问的代码使用包私有可见性。

Regarding using separate root directories, that is a good practice. 关于使用单独的根目录,这是一个很好的做法。 It also has an advantage for us, since we use IDEA, IDEA recognizes that production code cannot reference test code. 它对我们也有好处,因为我们使用IDEA,IDEA认识到生产代码不能引用测试代码。

In terms of keeping them separate, there is a great power in having one, and only one, test class per production class at the unit level. 在保持它们分离方面,在单元级别的每个生产类别中只有一个且只有一个测试类是很强大的。 Of course, some classes get created in production as part of refactoring that have no test classes at all, and that is fine, but when you want to know what test tests a certain class, having a convention that says ClassNameTest is the tests for ClassName is very helpful. 当然,有些类作为重构的一部分在生产中创建,根本没有测试类,这很好,但是当你想知道什么测试测试某个类时,有一个名为ClassNameTest的约定是ClassName的测试非常有帮助。

TestNG is much friendlier to this paradigm than JUnit, though. 不过,TestNG对这种范式比JUnit更友好。

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

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