Han*_*örr 92 java junit unit-testing parameterized-unit-test
在我们的项目中,我有几个JUnit测试,例如从目录中获取每个文件并对其进行测试.如果我实现了一个testEveryFileInDirectory方法,TestCase那么只显示一个可能失败或成功的测试.但我对每个文件的结果感兴趣.如何在每个文件中编写一个TestCase/ TestSuite如此单独的测试,例如在Eclipse的图形化TestRunner中?(为每个文件编写显式测试方法不是一种选择.)
bru*_*nde 99
看看JUnit 4 中的参数化测试.
实际上我几天前这样做了.我会试着解释一下......
首先正常构建您的测试类,就像您只使用一个输入文件进行测试一样.用以下方法装饰你的课程:
@RunWith(Parameterized.class)
Run Code Online (Sandbox Code Playgroud)
构建一个构造函数,该构造函数接受将在每次测试调用中更改的输入(在这种情况下,它可能是文件本身)
然后,构建一个返回Collection数组的静态方法.集合中的每个数组都将包含类构造函数的输入参数,例如文件.用以下方法装饰此方法:
@Parameters
Run Code Online (Sandbox Code Playgroud)
这是一个示例类.
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
另请查看此示例
McD*_*ell 27
JUnit 3
public class XTest extends TestCase {
public File file;
public XTest(File file) {
super(file.toString());
this.file = file;
}
public void testX() {
fail("Failed: " + file);
}
}
public class XTestSuite extends TestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("XTestSuite");
File[] files = new File(".").listFiles();
for (File file : files) {
suite.addTest(new XTest(file));
}
return suite;
}
}
Run Code Online (Sandbox Code Playgroud)
JUnit 4
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class TestY {
@Parameters
public static Collection<Object[]> getFiles() {
Collection<Object[]> params = new ArrayList<Object[]>();
for (File f : new File(".").listFiles()) {
Object[] arr = new Object[] { f };
params.add(arr);
}
return params;
}
private File file;
public TestY(File file) {
this.file = file;
}
@Test
public void testY() {
fail(file.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
Junit 5参数化测试
JUnit 5参数化测试通过允许使用方法作为数据源来支持这一点:
@ParameterizedTest
@MethodSource("fileProvider")
void testFile(File f) {
// Your test comes here
}
static Stream<File> fileProvider() {
return Arrays.asList(new File(".").list()).stream();
}
Run Code Online (Sandbox Code Playgroud)
JUnit 5 DynamicTests
JUnit 5也通过静态方法DynamicTest在a中生成a 的概念来支持这一点.@TestFactorydynamicTest
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import java.util.stream.Stream;
@TestFactory
public Stream<DynamicTest> testFiles() {
return Arrays.asList(new File(".").list())
.stream()
.map((file) -> dynamicTest(
"Test for file: " + file,
() -> { /* Your test comes here */ }));
}
Run Code Online (Sandbox Code Playgroud)
在IDE(此处为IntelliJ)中运行的测试将显示如下:
| 归档时间: |
|
| 查看次数: |
33066 次 |
| 最近记录: |