Conditionally Ignoring Tests in Junit 4

Conditionally ignoring tests in JUnit 4

The JUnit way is to do this at run-time is org.junit.Assume.

 @Before
public void beforeMethod() {
org.junit.Assume.assumeTrue(someCondition());
// rest of setup.
}

You can do it in a @Before method or in the test itself, but not in an @After method. If you do it in the test itself, your @Before method will get run. You can also do it within @BeforeClass to prevent class initialization.

An assumption failure causes the test to be ignored.

Edit: To compare with the @RunIf annotation from junit-ext, their sample code would look like this:

@Test
public void calculateTotalSalary() {
assumeThat(Database.connect(), is(notNull()));
//test code below.
}

Not to mention that it is much easier to capture and use the connection from the Database.connect() method this way.

Is there any way to conditionally ignore a test with JUnit of Spring?

With Junit Assume:

public class SomeTest {
@Before
public void before() throws Exception {
Assume.assumeTrue("someValue".equals(System.getProperty("some.property")));
}

// add your @Test
}

You could also use @IfProfileValue for tests using spring junit runner

JUnit running test methods conditionally

Use JUnit's assume mechanism described here. You will need to use either Theories of Parameterized to cause JUnit to execute more than once if you want to drive those two conditions.

JUnit 4 - How to ignore all package with the tests?

You can mention on your build.gradle what packages to exclude from tests

test {
exclude '**/*IntegrationTest*'
}

same for maven:

must consider this notation:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

"**/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>

Another option is the old

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

or even when call it

mvn install -DskipTests

is there any conditional annotation in JUnit to mark few test cases to be skipped?

Hard to know if it is the @Ignore annotation that you are looking for, or if you actually want to turn off certain JUnit tests conditionally. Turning off testcases conditionally is done using Assume.

You can read about assumptions in the release notes for junit 4.5

There's also a rather good thread here on stack over flow:
Conditionally ignoring tests in JUnit 4

How to execute test classes conditionally using Junit 4.11

I usually use the assumeTrue in JUnit4. Maybe this is an option for you.

@Test
public void testOnlyWhenConditionTrue() {
assumeTrue(conditionTrue);
... // your test steps
}

Assumptions and Conditional Test Execution with JUnit 4 and 5

How to get Junit 4 to ignore a Base Test Class?

Use to @Ignore annotation. It also works on classes.
See this one:

@Ignore public class IgnoreMe {
@Test public void test1() { ... }
@Test public void test2() { ... }
}

Also, you can annotate a class
containing test methods with @Ignore
and none of the containing tests will
be executed.

Source: JUnit JavaDoc

How can I automatically skip certain JUnit tests based on a condition?

The exception thrown by a TestWatcher.starting are ignored, and rethrown at the end of the test.

You should implement a TestRule instead of a TestWatcher :

public class TestRules implements TestRule {
private int priority = 1; // this value is manually changed to set the priority of tests to run

public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Priority testCasePriority = desc.getAnnotation(Priority.class);
Assume.assumeTrue("Test skipped for priotity " + priority, testCasePriority == null || testCasePriority.value() <= priority);

base.evaluate();
}
};
}
}


Related Topics



Leave a reply



Submit