我应该把我的 JUnit 测试放在哪里? [英] Where should I put my JUnit tests?

查看:29
本文介绍了我应该把我的 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天全站免登陆