JUnit optional / required tests

I have Junit 4.8.2 and maven 3. Some tests in my application should disable the assembly in case of failure, and some of them should not (just let me know that the following additional tests failed to complete)

How can I do this with junit, and if I can't, maybe testng can?

eg. I have two test cases:

Firstly, this is not very important and may be unsuccessful due to a connection timeout, service unavailability, etc. and so on. So if this fails, I don’t want to give up the whole assembly, just to inform the user about it and write to the console

Secondly, it is really important, and if it does not work, the assembly must also fail.

I know about @Ignore - this is not what I am looking for because I do not want to miss any tests.

I know about @Category, so if you know how to configure the surefire plugin to say: if category com.me.Required - the assembly should fail in case of failure and if the category com.me.Optional - build should not fail

+7
source share
2 answers

Consider using the failsafe plugin for your tests that are allowed to fail, and set testFailureIgnore to true.

To use a secure plugin, you must add the plugin to you 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> 

The surefire plugin will run by default, for example, Test . The failover plugin will by default run a test called IT .

Given the tests

 import static org.junit.Assert.*; import org.junit.Test; public class SurefireTest { @Test public void test() { assertTrue(true); } } 

and

 import static org.junit.Assert.*; import org.junit.Test; public class FailsafeIT { @Test public void test() { assertTrue(false); } } 

Running mvn install will now result in

 [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building test 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] . . . ------------------------------------------------------- TESTS ------------------------------------------------------- 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 . . . ------------------------------------------------------- TESTS ------------------------------------------------------- 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] ------------------------------------------------------------------------ 
+3
source
+1
source

All Articles