JUnit 参数化测试:如何仅从 IntelliJ/Eclipse 运行 1 个特定测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16517300/
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-09-19 20:29:14  来源:igfitidea点击:

JUnit parameterized tests: how do I run only 1 specific test from IntelliJ/Eclipse?

javaeclipsejunitintellij-idea

提问by Geoffrey De Smet

I have a @Parameterizedjunit test that spawns 50 tests:

我有一个@Parameterized生成 50 个测试的junit 测试:

@RunWith(Parameterized.class)
public class NurseRosteringSolveAllTurtleTest ... {

    @Parameterized.Parameters(name = "{index}: {0}")
    public static Collection<Object[]> getSolutionFilesAsParameters() {
        return ... // returns 50 Files.
    }

    public NurseRosteringSolveAllTurtleTest(File unsolvedDataFile) {
        ...
    }

    ...

    @Test
    public void solveDataFile() {
        ...
    }

}

Running it takes an hour (and it's impossible to shorten that time, they are integration tests). Test 28 fails.

运行它需要一个小时(并且不可能缩短该时间,它们是集成测试)。测试 28 失败。

How do I run test 28 alone, without running the other 49 tests? Without changing the actual code, by simply configuring a -Dor something similar in IntelliJ's (or Eclipse's) run configuration.

如何单独运行测试 28,而不运行其他 49 个测试?在不更改实际代码的情况下,只需-D在 IntelliJ(或 Eclipse)的运行配置中配置一个或类似的东西。

采纳答案by Moritz Eysholdt

Eclipse is now (as of the Mars M4 release) able to run not just a single test from the Parameterized test class but any kind of subtree.

Eclipse 现在(从 Mars M4 版本开始)不仅可以运行来自参数化测试类的单个测试,还可以运行任何类型的子树。

This can be:

这可以是:

  • all methods for a single data set as returned by the @Parameterized-method
  • all datasets for a single @Test-method
  • @Parameterized-method 返回的单个数据集的所有方法
  • 单个@Test-method 的所有数据集

And as already mentioned, the test can also be specified by entering the tests name into the "method" text filed within the launch configuration. There will be a marker indicating that the method doesn't exist, but the test will run anyway.

如前所述,还可以通过将测试名称输入到启动配置中的“方法”文本中来指定测试。会有一个标记表明该方法不存在,但无论如何测试都会运行。

See this blog postfor details.

有关详细信息,请参阅此博客文章

回答by Duncan Jones

I just tested this in Eclipse with a simple parameterized test that always fails on test #4. One is able to right-click on the failed test and select Run. Only that test then executes.

我刚刚在 Eclipse 中使用一个简单的参数化测试对此进行了测试,该测试在测试 #4 中总是失败。可以右键单击失败的测试并选择Run。然后仅执行该测试。

test output

测试输出

Result:

结果:

just test 4

只测试 4

Frustratingly, I can't see what Eclipse did to solve the problem. Nothing is apparently altered in the run configuration. In particular, if you select to run the configuration a second time, it executes all the tests.

令人沮丧的是,我看不到 Eclipse 做了什么来解决这个问题。运行配置中没有明显改变。特别是,如果您选择第二次运行配置,它将执行所有测试。

Some further testing shows that Eclipse will regenerate all 10 parameter values, but only uses the 4th value. (This was determined by embedding a print statement in the @Parametersmethod).

一些进一步的测试表明 Eclipse 将重新生成所有 10 个参数值,但仅使用第 4 个值。(这是通过在@Parameters方法中嵌入打印语句来确定的)。

回答by duemir

Not sure if it will help, but you can try a trick which I used with Eclipse and JUnit parameterized tests.

不确定它是否会有所帮助,但您可以尝试我在 Eclipse 和 JUnit 参数化测试中使用的技巧。

In JUnit launch configuration in "Test method" field you can write the full name of parameterized test, in your example it should be something like this 'solveDataFile[28: /path/to/your/file]'. Eclipse will complain that method does not exist but will still lunch it successfully.

在“测试方法”字段中的 JUnit 启动配置中,您可以编写参数化测试的全名,在您的示例中,它应该类似于“solveDataFile[28: /path/to/your/file]”。Eclipse 会抱怨方法不存在,但仍会成功午餐。

回答by Miguel Pereira

For a subset of tests ex( 27 & 28 ) Just add:

对于测试的子集 ex( 27 & 28 ) 只需添加:

`.subList( startInclusive, stopExclusive );`

before returning your parameters collection.

在返回参数集合之前。

Non consecutive subsets:

非连续子集:

Collection<Object[]> c = Arrays.asList( data ).subList( startInclusive, stopExclusive );
c.add( another subset );
return c;

回答by user7610

Similarly to Miguel's answer, if you are using the JUnit 5's

与 Miguel 的回答类似,如果您使用的是 JUnit 5

@ParameterizedTest
@CsvFileSource(resources = arrayOf("/sender.csv"))

you can go to your csv file and "comment out" some lines by prepending the #character to them.

您可以转到您的 csv 文件并通过将#字符添加到其中来“注释掉”一些行。