JUnit可选/必需的测试

时间:2012-09-24 10:24:09

标签: junit maven-3 testng junit4 maven-surefire-plugin

我有Junit 4.8.2和maven 3 我的应用程序中的某些测试应该在构建失败的情况下失败,其中一些不应该(仅报告以下可选测试失败)

如何使用junit进行操作,如果我不能,那么可以使用junng?

E.g。我有两个测试用例:

首先不是很重要,因为连接超时,服务不可用等原因而失败。因此,如果它失败了,我不想让整个构建失败,只是让用户知道它并写入控制台

第二个非常重要,如果失败,那么构建也应该失败

我知道@Ignore - 这不是我想要的,因为我不想跳过任何测试。

我知道@Category所以如果你知道如何配置surefire插件说:if category com.me.Required - 如果失败并且类别com.me.Optional - build不应该失败,build应该失败

2 个答案:

答案 0 :(得分:3)

考虑使用failsafe插件进行允许失败的测试,并将testFailureIgnore标志设置为true。

use故障安全插件,您必须将插件添加到pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foo.bar</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

surefire插件将默认执行名为Test的测试。 failafe插件默认执行名为IT的测试。

鉴于测试

import static org.junit.Assert.*;

import org.junit.Test;

public class SurefireTest {

    @Test
    public void test() {
        assertTrue(true);
    }

}

import static org.junit.Assert.*;

import org.junit.Test;

public class FailsafeIT {

    @Test
    public void test() {
        assertTrue(false);
    }

}

现在运行mvn install会产生

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
.
.
.
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running SurefireTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
.
.
.
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running FailsafeIT
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.072 sec <<< FA
ILURE!
...
Results :

Failed tests:   test(FailsafeIT)

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
.
.
.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.174s
[INFO] Finished at: Sat Sep 29 08:19:38 CEST 2012
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------

答案 1 :(得分:1)