看看JUnit 4 中的 参数化测试 。
实际上,我几天前就这样做了。我会尽力解释…
首先,正常构建测试类,就像在一个输入文件中进行测试一样。用以下方式装饰您的课程:
@RunWith(Parameterized.class)
构建一个构造函数,该构造函数接受在每次测试调用中都会更改的输入(在这种情况下,它可能是文件本身)
然后,构建一个静态方法,该方法将返回一个
Collection数组。集合中的每个数组都将包含类构造函数的输入参数,例如文件。用以下方法装饰此方法:
@Parameters
这是一个示例类。
@RunWith(Parameterized.class)public class ParameterizedTest { private File file; public ParameterizedTest(File file) { this.file = file; } @Test public void test1() throws Exception { } @Test public void test2() throws Exception { } @Parameters public static Collection<Object[]> data() { // load the files as you want Object[] fileArg1 = new Object[] { new File("path1") }; Object[] fileArg2 = new Object[] { new File("path2") }; Collection<Object[]> data = new ArrayList<Object[]>(); data.add(fileArg1); data.add(fileArg2); return data; }}还要检查这个例子



