在我们的项目中我有几个JUnit http://www.junit.org/测试例如从目录中取出每个文件并对其运行测试。如果我实施一个testEveryFileInDirectory
方法中的TestCase
这显示为只有一项可能失败或成功的测试。但我对每个单独文件的结果感兴趣。我怎样才能写一个TestCase
/ TestSuite
这样每个文件都显示为单独的测试,例如在 Eclipse 的图形化 TestRunner 中? (为每个文件编写显式测试方法不是一种选择。)
也比较一下问题Eclipse Testrunner 中具有名称的 ParameterizedTest https://stackoverflow.com/questions/385925/parameterizedtest-with-a-name-in-eclipse-testrunner.
看一眼参数化测试在 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;
}
}
还要检查这个example http://www.nofluffjuststuff.com/blog/paul_duvall/2007/04/take_heed_of_mixing_junit_4_s_parameterized_tests
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)