19

I wrote a performance test class marked as @Ignore because it takes a very long time to run and I don't normally need to run it. I'd like to be able to right click one of the @Tests and say "run" but when I do that in intellij it doesn't run the test because the class is marked as ignored.

I don't want to comment out the @Ignore because it's too easy to accidentally commit the file with the @Ignore removed. But I feel that if I explicitly tell Intellij to run a test it should run it regardless of the @Ignore. Keep in mind, if I run a group of classes (eg: all in a package) I still would want this test class to be ignored.

Is it possible to get this functionality in Intellij?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 2
    in my opinion it is not a good idea to abuse the @Ignored annotation for this. maybe putting the test in a separate suite that you only run when you need it would be better. eventually (on your CI platform) you would want the test to execute anyways, because thats what tests are for. – kmera Jan 16 '14 at 23:41
  • @k-mera ah that's an interesting idea. It would take a lot more effort because I will have to make sure that my build script is configured to skip it, but I may go down that route. – Daniel Kaplan Jan 16 '14 at 23:43
  • yes I understand the issue. once you commit your code it should be tested anyways with all your tests. but if it takes a lot of time locally then this may slow down development. – kmera Jan 16 '14 at 23:45
  • 3
    I agree with @k-mera. This test should probably not be annotated with `@Ignore`, but with something like `@Category(SlowTest.class)` instead. Then a CI-only suite that uses `@IncludeCategory(SlowTests.class)` can be created. – Justin Muller Jan 16 '14 at 23:55
  • 2
    I agree with your desired behavior, Daniel, and the strange thing is that JetBrains' Test Runner in ReSharper behaves exactly that way. `@Ignore` being irrevocably ignored makes it useless, imho, might as well delete the test then – Arne Claassen Nov 25 '15 at 06:24

1 Answers1

6

I found this link which says that you can do something tricky with Assume.assumeTrue that will mark the test as ignored if the condition is false, and normally this is a system property that is used in the condition, so you can pass it in as a parameter on the command line. IntelliJ lets you customize the command line parameters, so it should work, but I haven't tried it myself.

The example from the link is:

@Test
public void shouldTryEveryPossiblePhoneticAttributeSet() throws IOException {
    Assume.assumeTrue(TestEnvironment.hasBigParseSets());
    ...
}

public class TestEnvironment {
   private static final String HAS_BIG_PARSESETS = "hasBigParseSets";
   public static boolean hasBigParseSets(){
      return "true".equalsIgnoreCase(System.getProperty(HAS_BIG_PARSESETS));
   }
}

And "mvn test -P bigParseSets" vs "mvn test".

edit: And I just found this neat thread on StackOverflow that tells how to run a single junit test. I assume I don't need to quote that since it's to a post here on StackOverflow. That explains how to do it from a command line, but you should be able to do something very similar to that one that has hard coded values for the class and method names, and then just right click on the SingleJUnitTestRunner class and ask IntelliJ to run it.

Community
  • 1
  • 1
Teresa Carrigan
  • 668
  • 8
  • 14
  • 1
    That's a good trick but it doesn't seem to work the way one would want on Intellij. For some reason, `Assume.assumeTrue(false);` returns an error instead of being ignored, [like the documentation says it does, by default](http://junit.sourceforge.net/javadoc/org/junit/Assume.html). – Daniel Kaplan Jan 17 '14 at 18:29
  • What's really weird about what I just said is that your link shows screenshots saying otherwise. I looked into it further and discovered that this is because I'm using [junit benchmarks](http://labs.carrotsearch.com/junit-benchmarks.html). I guess that test runner doesn't handle `Assume` the way you'd want. – Daniel Kaplan Jan 17 '14 at 18:40