java 是否可以忽略某些单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1593203/
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
Is it possible to ignore certain unit tests?
提问by James Camfield
I'm currently working on a project which uses JUnit4 extensively throughout all the modules. We're using Maven2 to build the project and Hudson for continuous integration. The project is built and run under Java 1.5.
我目前正在开发一个在所有模块中广泛使用 JUnit4 的项目。我们使用 Maven2 来构建项目,并使用 Hudson 进行持续集成。该项目是在 Java 1.5 下构建和运行的。
We've recently added quite a large raft of unit tests which are needed at times, but don't want to be run during the build process (but other unit tests in the same project do).
我们最近添加了大量有时需要的单元测试,但不想在构建过程中运行(但同一项目中的其他单元测试会这样做)。
I want to know if there's some kind of annotation that can be applied to the tests, or some configuration that can be made to Maven that would ignore unit tests that live under a certain package or in a certain test class at build time?
我想知道是否有某种注释可以应用于测试,或者可以对 Maven 进行一些配置,这些配置会在构建时忽略位于某个包或某个测试类中的单元测试?
There's possibly the alternative of just putting these particular tests in a normal class that would be run through main, but that's not ideal.
可能还有一种选择,就是将这些特定的测试放在一个通过 main 运行的普通类中,但这并不理想。
Thanks for your suggestions
谢谢你的建议
回答by cetnar
You can configure maven-surefire-plugin.
您可以配置maven-surefire-plugin。
For example:
例如:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
EDIT:
Another solution is to define profilesin which all test will be run e.g. by hudson. In development mode you can use subset of all test to speed up development (or all test might be in default profile, choosen test in dev profile - so you need run maven with -PprofileNameattribute). This example better suits for integration tests that take loger to run.
编辑:
另一个解决方案是定义配置文件,所有测试都将在其中运行,例如由 hudson 运行。在开发模式下,您可以使用所有测试的子集来加速开发(或者所有测试可能在默认配置文件中,在开发配置文件中选择测试 - 所以您需要使用-PprofileName属性运行 maven )。这个例子更适合需要 loger 运行的集成测试。
回答by user85421
There is an "Ignore" annotation, but it's manual workto add them, not sure if that helps.
It can be used for a test method or a whole class
有一个“忽略”注释,但添加它们是手动工作,不确定是否有帮助。
它可以用于测试方法或整个类
@Ignore("not ready yet") @Test public void testSomething() {...
or
或者
@Ignore public class SomeTest {
@Test public void testSomething() {...
}
[]]
[]]
回答by ptomli
Try JUnit Assumptions, which seem to be since 4.4
尝试 JUnit 假设,这似乎是从 4.4 开始的
Presuming you can detect at runtime if the tests should be run (via defines, properties, etc), this will allow you to keep a nice clean test suite.
假设您可以在运行时检测是否应该运行测试(通过定义、属性等),这将允许您保持一个干净的测试套件。
回答by Hosam Aly
I never tried it, but could you put the additional tests in a different source folder, and configure your build script to include or exclude it according to your build target?
我从未尝试过,但是您能否将其他测试放在不同的源文件夹中,并根据您的构建目标配置您的构建脚本以包含或排除它?
回答by denis.zhdanov
回答by sfussenegger
Maven's Surefire Plugin: Inclusions and Exclusions of Tests
Maven 的 Surefire 插件:测试的包含和排除
回答by Michael Lloyd Lee mlk
JUnit 4.7 I believe supports "Rules" for this kind of thing (pre 4.7 I think you could have used custom Runners who would check an environment variable).
JUnit 4.7 我相信支持这种事情的“规则”(在 4.7 之前,我认为您可以使用自定义 Runners 来检查环境变量)。
Or you could look at the includes/excludes tags of your build system.
或者您可以查看构建系统的包含/排除标签。
回答by abhijitcaps
You need to add the following into your POM.xml
您需要将以下内容添加到 POM.xml 中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Check out this URL: http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html
查看此 URL:http: //maven.apache.org/surefire/maven-surefire-plugin/examples/skiping-test.html
--Abhijit Das
——阿比吉特·达斯

![java 从 List<Byte> 创建一个 byte[]](/res/img/loading.gif)