运行 多次特定 JUnit 测试,但多次从 运行 中排除其他测试

Run specific JUnit tests multiple times, but exclude others from running multiple times

我多次使用 Parameterized JUnit 运行ner 来 运行 我的一些测试。这是我测试的模板 class

@RunWith(value = Parameterized.class)
public class TestClass {

    private String key;
    private boolean value;

    public TestClass(String key, boolean value) {
        this.key = key;
        this.value = value;
    }

    @Parameters
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][] {
            {"key1", true},
            {"key2", true},
            {"key3", false}
        };
        return Arrays.asList(data);
    }

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

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

    @Test
    public void testNotRelatedKeyValue() {
    }
}

现在,我想要我的测试方法 - testKeys(), testValues() 到 运行 具有不同的参数值,它们是 运行ning.

但是,我的最后一个方法 - testNotRelatedKeyValue() 也与其他参数化测试一起执行了那么多次。

我不想 testNotRelatedKeyValue() 到 运行 多次,只希望一次。

在这个 class 中是否可行,或者我是否需要创建一个新测试 class?

您可以使用 Enclosed runner 来构建您的测试。

@RunWith(Enclosed.class)
public class TestClass {

    @RunWith(Parameterized.class)
    public static class TheParameterizedPart {
        @Parameter(0)
        public String key;

        @Parameter(1)
        private boolean value;

        @Parameters
        public static Object[][] data() {
            return new Object[][] {
                {"key1", true},
                {"key2", true},
                {"key3", false}
            };
        }

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

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

    public static class NotParameterizedPart {
        @Test
        public void testNotRelatedKeyValue() {
            ...
        }
    }
}