混合参数化和程序化单元测试

时间:2015-11-18 01:21:01

标签: java junit parameterized

使用JUnit,您可以使用@RunWith(Parameterized.class)提供一组参数传递给测试构造函数,然后对每个对象运行测试。

我试图将尽可能多的测试逻辑移植到数据中,但是有些测试不能轻易转换为数据驱动的测试。有没有办法使用JUnit的Parameterized运行器来运行一些带参数的测试,然后还添加不为每个测试对象构造重复运行的非数据驱动的测试?

2 个答案:

答案 0 :(得分:1)

我的解决方法是创建一个单独的类,并将程序化和数据驱动的测试放在两个独立的子类中。子类必须是静态的,以便JUnit运行其测试。这是一个骨架:

@RunWith(Enclosed.class) // needed for working well with Ant
public class MyClassTests {
    public static class Programmatic {
        @Test
        public void myTest(){
            // test something here
        }
    }

    @RunWith(Parameterized.class)
    public static class DataDriven {
        @Parameters
        public static Collection<Object[]> getParams() {
            return Collections.emptyList();
        }

        private String data;
        public DataDriven(String testName, String data){
            this.data = data;
        }
        @Test
        public void test() throws AnalyzeExceptionEN{
            // test data string here
        }
    }
}

答案 1 :(得分:1)

一种方法是使用Junit的body { padding-top: 50px; padding-bottom: 20px; } /* Set padding to keep content from hitting the edges */ .body-content { padding-left: 15px; padding-right: 15px; } /* Set width on the form input elements since they're 100% wide by default */ input, select, textarea { max-width: 280px; } .firstNumber { color: red; } .secondNumber { color: blue; } .thirdNumber { color: green; } .fourthNumber { color: orange; } .fifthNumber { color: purple; } 跑步者。它非常冗长但也非常强大。它允许您在一个文件中组合多个不同的运行器。

其他选项是使用自定义junit runner。 zohhak肯定支持tests with parameters and without。小提取物:

Enclosed

如果这对你来说还不够,你肯定可以找到支持这两种测试的其他跑步者。

相关问题