JUnit 4-如何忽略所有带有测试的软件包?

Kir*_* Ch 1 java junit

我在一个大项目中运行JUnitMockito测试。我使用它们来测试连接到Web服务的服务器端组件。所有这些连接都需要一些时间,并且在构建期间不必执行它们。

我希望在构建期间忽略我的测试。

我大约有10堂课有测试。因此,显而易见的方法是使用@Ignore注释所有类。但是,每当我将代码提交到项目中然后重新注释所有测试时,都应该执行此操作。我认为这不是最好的解决方案。

那么这是否可能以某种方式简单地忽略了测试的所有包(比如说com.example.tests)?或者以简单的方式忽略构建中的测试的解决方案是什么?

Gui*_*rme 6

您可以在build.gradle上提及要从测试中排除哪些软件包

test {
    exclude '**/*IntegrationTest*'
}
Run Code Online (Sandbox Code Playgroud)

同样的行家:

必须考虑以下符号:

默认情况下,Surefire插件将自动包含具有以下通配符模式的所有测试类:

"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
"**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
"**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".


<project>
  [...]
 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <excludes>
                <exclude>*com.example.tests*/*Test.java</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)

另一种选择是旧

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

甚至当它叫

mvn install -DskipTests
Run Code Online (Sandbox Code Playgroud)