简体   繁体   English

将JUnit类分离成特殊的测试包?

[英]Separation of JUnit classes into special test package?

I am learning the concepts of Test-Driven Development through reading the Craftsman articles (click Craftsman under By Topic ) recommended in an answer to my previous question, "Sample project for learning JUnit and proper software engineering" . 我正在学习测试驱动开发的概念,通过阅读我之前的问题“用于学习JUnit和适当的软件工程的示例项目”的回答中推荐的Craftsman文章 (单击By Topic下的Craftsman )。 I love it so far! 到目前为止我喜欢它!

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? 您如何组织JUnit测试类和实际代码? I'm talking mainly about the package structure, but any other concepts of note would be helpful too. 我主要谈论的是包结构,但任何其他注意事项也都会有所帮助。

Do you put test classes in org.myname.project.test.* and normal code in org.myname.project.*? 您是否将测试类放在org.myname.project.test。*和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? 您更喜欢使用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 almost the 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! 编辑:我完全忘记了Maven,但似乎大多数人都在使用它! 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. 在过去,我有一个特定的用例,Maven完全打破了我,但Ant给了我所需的灵活性,所以我最终依附于Ant,但我想也许我只是采取了错误的方法。 I think I'll give Maven another try because it sounds like it will go well with test-driven development. 我想我会再给Maven一个尝试,因为听起来它会很好地适应测试驱动的开发。

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. 如上例所示,我的测试类具有测试类的名称加上Test作为后缀。 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开头...

Update inspired 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. 受@ Ricket评论启发的更新 :这种方式测试类(通常)在他们的测试伙伴之后立即出现在项目名称的类名列表中。 (Funny that I am benefiting from this day by day, without having consciously realized how...) (有趣的是,我从一天中受益,没有有意识地意识到......)

Update2: A lot of developers (including myself) like Maven, but there seems to be at least as many who don't. Update2:很多开发人员(包括我自己)都喜欢Maven,但似乎至少有很多人没有。 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). 恕我直言,它对“主流”Java项目非常有用(我会将约90%的项目纳入此类别......但其他10%仍然是相当大的少数项目)。 It is easy to use if one can accept the Maven conventions; 如果可以接受Maven惯例,它很容易使用; 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. 对于许多在Ant上社交的人来说,Maven似乎很难理解,因为它显然需要一种非常不同的思维方式。 (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. (我自己,从未使用过Ant,无法比较两者。)有一件事是肯定的:它使单元(和集成)测试成为流程中自然的,一流的步骤,这有助于开发人员采用这种基本实践。

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. 以这种方式组织我的测试代码允许我轻松地单独编译和打包它,以便生产jar文件不包含测试代码。 It also allows the test code to access package private fields and methods. 它还允许测试代码访问包私有字段和方法。

I use Maven . 我用Maven The structure that Maven promotes is:- Maven推出的结构是: -

src/main/java/org/myname/project/MyClass.java

src/test/java/org/myname/project/TestMyClass.java

ie 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. 将测试类放在同一个包中(不一定是目录)的一个优点是,您可以利用包范围方法来检查或注入模拟测试对象。

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

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