Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make JUnit ignore specific test case at run time when run with Parameterized runner? [duplicate]

Tags:

java

junit

Possible Duplicate:
Conditionally ignoring tests in JUnit 4

I have a suite of system tests that are run with Parameterized runner and that I would like to run against different environments. Some of the tests should only run in non-production environments, and when run in production I would like to see them as ignored, so I'm looking for a way to say:

@Test
public void dangerousTest() {
    if (isProduction(environment)) ignore(); // JUnit doesn't provide ignore()
    environment.launchMissilesAt(target);
    assertThat(target, is(destroyed()));
}

The issue is that JUnit doesn't provide ignore() method to ignore a test case at run time. Furthermore, Assume.assumeTrue(isNotProduction(environment)) doesn't seem to work with Parameterized runner -- it simply marks the tests as passed, not ignored. Any ideas how something equivalent can be achieved with the constraints that:

  • the suite needs to use Parameterized runner
  • the tests need to appear as ignored if the suite is run in production?
like image 384
narthi Avatar asked Jul 05 '11 14:07

narthi


People also ask

How do you ignore a specific test case?

The @Ignore test annotation is used to ignore particular tests or group of tests in order to skip the build failure. @Ignore annotation can be used in two scenarios as given below: If you want to ignore a test method, use @Ignore along with @Test annotation.

Which of the annotation make it possible to run a test multiple times with different parameters?

@ParameterizedTest. Parameterized tests make it possible to run a test multiple times with different arguments. They are declared just like regular @Test methods but use the @ParameterizedTest annotation instead.


1 Answers

You could always do

if (isProduction(environment)) return;

The test would be marked as passed, but at least the missiles wouldn't be launched.

Or you could use Assume, which I've never used, but is documented this way:

A set of methods useful for stating assumptions about the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information. The default JUnit runner treats tests with failing assumptions as ignored. Custom runners may behave differently. For example:

Your code would thus look like this:

@Test
public void dangerousTest() {
    assumeTrue(!isProduction(environment));
    // ...
}
like image 90
JB Nizet Avatar answered Oct 12 '22 22:10

JB Nizet