Java/JUnit过滤重复条件

bud*_*udi 2 java junit

我的具体问题是关于JUnit的参数化测试,如果它包含某个属性,则过滤(基本上不运行)测试.例如:

@Test
public void test1() {
    if (property.contains("example")) {
        return;
    }
    assertEquals(expected, methodToTest1(actual));
}

@Test
public void test2() {
    if (property.contains("example")) {
        return;
    }
    assertEquals(expected, methodToTest2(actual));
}
Run Code Online (Sandbox Code Playgroud)

问题是,是否存在一种技术,其中约束if (property.equals("example"))...在其他地方静态定义,而不是在每个测试方法之前?像这样:

/** define constraint "property.equals("example")" somewhere **/

@Test
public void test1() {
    assertEquals(expected, methodToTest1(actual));
}

@Test
public void test2() {
    assertEquals(expected, methodToTest2(actual));
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ner 5

您可以使用JUnit的假设功能一起@Before.

@Before向测试类添加方法

@Before
public void dontRunIfExample() {
  assumeFalse(property.contains("example"));
}
Run Code Online (Sandbox Code Playgroud)

if从每个测试中删除块.