Java 我可以强制 Intellij-IDEA 运行一个被忽略的测试吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21175148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:07:20  来源:igfitidea点击:

Can I force Intellij-IDEA to run an ignored test?

javajunitintellij-ideajunit4

提问by Daniel Kaplan

I wrote a performance test class marked as @Ignorebecause 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.

我写了一个性能测试类,@Ignore因为它需要很长时间才能运行,而且我通常不需要运行它。我希望能够右键单击其中一个@Tests 并说“运行”,但是当我在 intellij 中执行此操作时,它不会运行测试,因为该类被标记为已忽略。

I don't want to comment out the @Ignorebecause it's too easy to accidentally commit the file with the @Ignoreremoved. 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.

我不想注释掉 ,@Ignore因为很容易不小心将文件与@Ignore删除的内容一起提交。但我觉得如果我明确告诉 Intellij 运行测试,它应该运行它,而不管@Ignore. 请记住,如果我运行一组类(例如:全部在一个包中),我仍然希望忽略这个测试类。

Is it possible to get this functionality in Intellij?

是否可以在 Intellij 中获得此功能?

采纳答案by Teresa Carrigan

I found this linkwhich 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.

我发现这个链接说你可以用 Assume.assumeTrue 做一些棘手的事情,如果条件为假,它会将测试标记为忽略,通常这是一个在条件中使用的系统属性,所以你可以将它作为命令行上的一个参数。IntelliJ 允许您自定义命令行参数,因此它应该可以工作,但我自己还没有尝试过。

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".

以及“mvn test -P bigParseSets”与“mvn test”。

edit: And I just found this neat threadon 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.

编辑:我刚刚在 StackOverflow 上发现了这个简洁的线程,它告诉了如何运行单个 junit 测试。我想我不需要引用它,因为它是 StackOverflow 上的一篇文章。这解释了如何从命令行执行此操作,但您应该能够执行与具有类和方法名称硬编码值的操作非常相似的操作,然后只需右键单击 SingleJUnitTestRunner 类并要求 IntelliJ 运行它。