我应该在哪里进行JUnit测试? [英] Where should I put my JUnit tests?

查看:94
本文介绍了我应该在哪里进行JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个关于组织单元测试的问题。

I've got 2 questions about organising Unit tests.


  1. 我是否必须将测试放到同一个包中测试类,或者我可以在不同的包中组织测试吗?

  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?

模拟和存根类怎么样?我应该将它们与仅包含测试的包分开,还是将它们放在一起?

What about mock and stub classes? Shall I separate them from packages containing only tests, or put them together?


推荐答案

<我们执行JUnit测试用例的方法是将它们放在同一个包中,但是放在不同的根目录中。由于我们使用Maven,我们只使用标准位置,使结构类似于以下内容。

The way we do our JUnit test cases is to put them in the same package, but in a different root directory. Since we use Maven, we just use the standard locations making the structure similar to the following.

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

显然,结构还有更多,但这可以让我们与主线代码分开构建测试,但仍然可以访问受保护的类等。对于不同类型的测试,这是非常主观的。当我们开始我们的测试工作(不幸的是在开发之后开始)时,我试图让事情相当孤立。不幸的是,当我们达到500+测试用例点时,它很快变成了一场噩梦。我以后试图做更多整合。这导致维护的代码量减少。正如我所说,这是非常主观的。

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. 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.

就测试代码而言,我们将其保存在一个单独的 com.foo.test 包中仅在 src / test / java 树中。

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.

这篇关于我应该在哪里进行JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆