Java 单元测试,目录布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1540324/
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
Java unit tests, directory layout
提问by Mike
When building a suite of unit tests for Java code, is there a convention for where to place test code in relation to source code?
在为 Java 代码构建一套单元测试时,是否有关于将测试代码放置在与源代码相关的位置的约定?
For example, if I have a directory /java
that contains a bunch of .java
source files, is it better to put the test cases in /java
itself or use something like /java/test
.
例如,如果我有一个/java
包含一堆.java
源文件的目录,是将测试用例放在/java
自身中还是使用类似/java/test
.
If the latter is preferred, how do you test the internals of the code when the private
/protected
members of a class aren't available outside the package?
如果后者是首选,当类的private
/protected
成员在包外不可用时,您如何测试代码的内部结构?
采纳答案by oxbow_lakes
You can put the tests in the same package as the original classes, even if the source code is under its own directory root:
您可以将测试放在与原始类相同的包中,即使源代码在其自己的目录根下:
PROJECT_ROOT
+--- src/
+----test/
You can declare a class com.foo.MyClass
under src
and its test com.foo.MyClassTest
under test
.
您可以com.foo.MyClass
在 下声明一个类src
并com.foo.MyClassTest
在 下声明它的测试test
。
As for access to private members, you can use reflectionto invoke the methods (altering their accessibility via Class.getDeclaredMethod.setAccessible
), or you could use something like testng/junit5 to put some annotation-driven tests on the source code itself (I personally think this is a bad idea).
至于访问私有成员,您可以使用反射来调用方法(通过 改变它们的可访问性Class.getDeclaredMethod.setAccessible
),或者您可以使用 testng/junit5 之类的东西在源代码本身上放置一些注释驱动的测试(我个人认为这是一个馊主意)。
Why not check out some projects on java.net
to see how they've organized things, for example swinglabs(the SVN repository is pretty slow I'm afraid)?
为什么不查看一些项目java.net
以了解他们是如何组织事物的,例如Swinglabs(恐怕 SVN 存储库非常慢)?
回答by OscarRyz
Most of the times is done like this:
大多数时候是这样完成的:
<SOME_DIR>/project/src/com/foo/Application.java
<SOME_DIR>/project/test/com/foo/ApplicationTest.java
So, you keep them separated and you can still test the package/protected functionality because the test is in the same package.
因此,您将它们分开,您仍然可以测试包/受保护的功能,因为测试在同一个包中。
You can't test private stuff, unless it is declared inside the class it self.
你不能测试私有的东西,除非它是在它自己的类中声明的。
Upon delivery, you just pack the .class
generated by src, not the tests
交付时,您只打包.class
src 生成的,而不是测试
回答by SingleShot
I recommend following the Apache Software Foundation's standard directory structure, which yields this:
我建议遵循Apache Software Foundation 的标准目录结构,它会产生以下结果:
module/
src/
main/
java/
test/
java/
This keeps tests separate from source, but at the same level in the directory structure. If you read through how Apache defines their structure, you'll see it helps partition other concerns out as well, including resources, config files, other languages, etc.
这使测试与源分开,但在目录结构中处于同一级别。如果您通读 Apache 如何定义它们的结构,您会发现它也有助于划分其他问题,包括资源、配置文件、其他语言等。
This structure also allows unit tests to test package and protected level methods of the units under test, assuming you place your test cases in the same package as what they test. Regarding testing private methods - I would not bother. Something else, either public, package, or protected calls them and you should be able to get full test coverage testing those things.
这种结构还允许单元测试测试被测单元的包和受保护级别的方法,假设您将测试用例与它们测试的内容放在同一个包中。关于测试私有方法 - 我不会打扰。其他东西,无论是公共的、包的还是受保护的,都可以调用它们,您应该能够获得完整的测试覆盖率来测试这些东西。
By the way, the link above is to Maven, Apache's standard build tool. Every Java project they have conforms to this standard, as well as every project I have encountered that is built with Maven.
顺便说一下,上面的链接是 Maven,Apache 的标准构建工具。他们拥有的每个 Java 项目,以及我遇到的每个使用 Maven 构建的项目都符合此标准。
回答by Alexander Pogrebnyak
Actually it makes a lot of sense to separate your Production and Test projects into 2 separate entities, but have the same package structure in both projects.
实际上,将您的生产和测试项目分成 2 个独立的实体是很有意义的,但在两个项目中具有相同的包结构。
So if I have a project 'my-project' I also create 'my-project-test', so I have the following directory structure:
所以如果我有一个项目“my-project”,我也会创建“my-project-test”,所以我有以下目录结构:
my-project
+--- src/com/foo
my-project-test
+---test/com/foo
This approach ensures that test code dependencies do not pollute production code.
这种方法确保测试代码依赖不会污染生产代码。
In my personal opinion, package private and protected methods should be tested as well as public methods. Hence I want my test classes in the same package as production classes.
在我个人看来,包私有和受保护的方法应该和公共方法一样进行测试。因此,我希望我的测试类与生产类位于同一个包中。
回答by Christian
This is how we have it set up and we like it.
这就是我们设置它的方式,我们喜欢它。
build/
src/
test/build/
test/src/
All testing code compiles into its own build directory. This is because we don't want production to contain test classes by mistake.
所有测试代码都编译到其自己的构建目录中。这是因为我们不希望生产错误地包含测试类。
回答by Ryszard D?egan
When creating a Java librarymodule in Android Studioit creates a default class under:
当创建一个Java库模块的Android工作室它创造下一个默认的类:
[module]
+ src/main/java/[com/foo/bar]
If you look into [module].iml
file, you will find that path as well as the path for tests, that you can utilize. The below is a summary:
如果您查看[module].iml
文件,您会发现该路径以及可以使用的测试路径。以下是总结:
<module>
<component>
<content>
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
</content>
</component>
</module>
What you can do in particular is to create a directory for tests to have the following structure:
您可以特别做的是为测试创建一个目录,使其具有以下结构:
[module]
+ src/main/java/[com/foo/bar]
+ src/test/java/[com/foo/bar]
The above structure will be recognized by Android Studioand your files underneath will be included into the module.
上面的结构会被Android Studio识别,你下面的文件会被包含到模块中。
I assume that that structure is a recommended layout for code and tests.
我假设该结构是代码和测试的推荐布局。