6

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?
kaya3
  • 47,440
  • 4
  • 68
  • 97
narthi
  • 2,188
  • 1
  • 16
  • 27

2 Answers2

5

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));
    // ...
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yes, but my requirement is for it to be marked as ignored by JUnit. – narthi Jul 05 '11 at 14:15
  • 1
    Thanks; I tried that but it seems the runner I use doesn't honour `AssumptionViolatedException`s. Question updated accordingly. – narthi Jul 05 '11 at 14:40
0

Two points:

With this release, a failed assumption will lead to the test being marked as passing, regardless of what the code below the assumption may assert. In the future, this may change, and a failed assumption may lead to the test being ignored: however, third-party runners do not currently allow this option.

I find this very counter intuitive as well, and even more frightening is the fact that this behavior might change in the future. I wish they had just held onto that feature until it worked as expected.

  • If using TestNG is an option, you could use groups for this. Put your test method in the "production" group:

    @Test(groups = "production")
    public void dangerousTest() { ... }
    

and when you are running your test, exclude the group "production".

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55