Java 将 JUnit 类分离成特殊的测试包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2388253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Separation of JUnit classes into special test package?
提问by Ricket
I am learning the concepts of Test-Driven Development through reading the Craftsman articles(click Craftsmanunder By Topic) recommended in an answer to my previous question, "Sample project for learning JUnit and proper software engineering". I love it so far!
我通过阅读学习测试驱动开发的概念工匠的文章(点击工匠下按主题)在回答中建议我刚才的问题,“学习JUnit和适当的软件工程样品工程”。到目前为止我喜欢它!
But now I want to sit down and try it myself. I have a question that I hope will need only a simple answer.
但现在我想坐下来自己尝试一下。我有一个问题,我希望只需要一个简单的答案。
How do you organize your JUnit test classes and your actual code?I'm talking mainly about the package structure, but any other concepts of note would be helpful too.
你如何组织你的 JUnit 测试类和你的实际代码?我主要谈论的是包结构,但任何其他值得注意的概念也会有所帮助。
Do you put test classes in org.myname.project.test.* and normal code in org.myname.project.*? Do you put the test classes right alongside the normal classes? Do you prefer to prefix the class names with Test rather than suffix them?
您是否将测试类放在 org.myname.project.test.* 中,将普通代码放在 org.myname.project.* 中?您是否将测试类与普通类放在一起?您是否更喜欢在类名前加上 Test 而不是后缀?
I know this seems like the kind of thing I shouldn't worry about so soon, but I am a very organization-centric person. I'm almostthe kind of person that spends more time figuring out methods to keep track of what to get done, rather than actually getting things done.
我知道这似乎是我不应该这么快担心的事情,但我是一个非常以组织为中心的人。我几乎是那种花更多时间找出方法来跟踪要完成的事情而不是实际完成事情的人。
And I have a project that is currently neatly divided up into packages, but the project became a mess. Instead of trying to refactor everything and write tests, I want to start fresh, tests first and all. But first I need to know where my tests go.
而且我有一个项目,目前被整齐地分成多个包,但该项目变得一团糟。与其试图重构一切并编写测试,我想重新开始,首先测试。但首先我需要知道我的测试去哪里了。
edit:I totally forgot about Maven, but it seems a majority of you are using it! In the past I had a specific use case where Maven completely broke down on me but Ant gave me the flexibility I needed, so I ended up attached to Ant, but I'm thinking maybe I was just taking the wrong approach. I think I'll give Maven another try because it sounds like it will go well with test-driven development.
编辑:我完全忘记了 Maven,但似乎你们中的大多数人都在使用它!过去我有一个特定的用例,Maven 完全崩溃了,但 Ant 给了我所需的灵活性,所以我最终依赖于 Ant,但我想也许我只是采取了错误的方法。我想我会再试一次 Maven,因为它听起来很适合测试驱动开发。
采纳答案by Péter T?r?k
I prefer putting the test classes into the same package as the project classes they test, but in a different physical directory, like:
我更喜欢将测试类与它们测试的项目类放在同一个包中,但放在不同的物理目录中,例如:
myproject/src/com/foo/Bar.java
myproject/test/com/foo/BarTest.java
In a Maven project it would look like this:
在 Maven 项目中,它看起来像这样:
myproject/src/main/java/com/foo/Bar.java
myproject/src/test/java/com/foo/BarTest.java
The main point in this is that my test classes can access (and test!) package-scope classes and members.
重点是我的测试类可以访问(和测试!)包范围的类和成员。
As the above example shows, my test classes have the name of the tested class plus Test
as a suffix. This helps finding them quickly - it's not very funny to try searching among a couple of hundred test classes, each of whose name starts with Test
...
如上例所示,我的测试类以被测试类的名称加上Test
作为后缀。这有助于快速找到它们 - 尝试在数百个测试类中搜索并不是很有趣,每个测试类的名称都以Test
...
Updateinspired by @Ricket's comment: this way test classes (typically) show up right after their tested buddy in a project-wise alphabetic listing of class names. (Funny that I am benefiting from this day by day, without having consciously realized how...)
受@Ricket 评论启发的更新:这种方式(通常)测试类(通常)在他们测试的伙伴之后立即显示在按项目字母顺序排列的类名称列表中。(有趣的是,我每天都从中受益,却没有意识到如何……)
Update2:A lot of developers (including myself) like Maven, but there seems to be at least as many who don't. IMHO it is very useful for "mainstream" Java projects (I would put about 90% of projects into this category... but the other 10% is still a sizeable minority). It is easy to use if one can accept the Maven conventions; however if not, it makes life a miserable struggle. Maven seems to be difficult to comprehend for many people socialized on Ant, as it apparently requires a very different way of thinking. (Myself, having never used Ant, can't compare the two.) One thing is for sure: it makes unit (and integration) testing a natural, first-class step in the process, which helps developers adopt this essential practice.
更新 2:很多开发人员(包括我自己)都喜欢 Maven,但似乎至少有很多人不喜欢。恕我直言,它对于“主流”Java 项目非常有用(我会将大约 90% 的项目归入这一类别……但另外 10% 仍然是相当多的少数)。如果可以接受Maven约定,则易于使用;然而,如果不是,它会使生活成为一场悲惨的斗争。对于在 Ant 上社交的许多人来说,Maven 似乎很难理解,因为它显然需要一种非常不同的思维方式。(我自己从未使用过 Ant,无法比较两者。)有一点是肯定的:它使单元(和集成)测试成为过程中自然的、一流的步骤,这有助于开发人员采用这一基本实践。
回答by Martin
I use Maven. The structure that Maven promotes is:-
我使用Maven。Maven 提倡的结构是:-
src/main/java/org/myname/project/MyClass.java
src/test/java/org/myname/project/TestMyClass.java
i.e. a test class with Test prepended to the name of the class under test is in a parallel directory structure to the main test.
即,在测试类名称前加上 Test 的测试类位于与主测试平行的目录结构中。
One advantage of having the test classes in the same package (not necessarily directory though) is you can leverage package-scope methods to inspect or inject mock test objects.
将测试类放在同一个包(虽然不一定是目录)中的一个优点是您可以利用包范围方法来检查或注入模拟测试对象。
回答by ChrisH
I put my test classes in the same package as what they are testing but in a different source folder or project. Organizing my test code in this fashion allows me to easily compile and package it separately so that production jar files do not contain test code. It also allows the test code to access package private fields and methods.
我将我的测试类放在与它们正在测试的相同的包中,但放在不同的源文件夹或项目中。以这种方式组织我的测试代码使我可以轻松地单独编译和打包它,以便生产 jar 文件不包含测试代码。它还允许测试代码访问包私有字段和方法。