我需要测试与YAML和JSON文件格式完全相同的系统。我为数据库后端编写了一堆单元测试,但我想同时在两种格式下运行它们。我需要更改的只是为测试提供的路径。我正在使用Java 8和org.junit.jupiter。
import static org.junit.jupiter.api.Assertions.*;
public class DatabaseTests {
//Need to re-test for "src\\test\\java\\backend\\database\\testDB.yaml"
private final static String TEST_DB_JSON = "src\\test\\java\\backend\\database\\testDB.json";
private static byte[] testFileState;
@BeforeAll
static void setUp() {
try {
testFileState = Files.readAllBytes(Paths.get(TEST_DB_JSON));
reloadDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
@AfterEach
void resetFile() {
try (FileOutputStream fos = new FileOutputStream(TEST_DB_JSON)) {
fos.write(testFileState);
} catch (IOException e) {
e.printStackTrace();
}
reloadDatabase();
}
//A bunch of unit tests
我不想只复制并粘贴整个类并仅更改一个变量,但是我无法弄清楚如何通过使类抽象化或其他方式来做到这一点。测试在两个文件上都相同(与我的数据库代码一样),并且两个文件包含完全相同的测试数据。
您可以使用jUnit5 Parametrized Tests:将为“ MethodSource”返回的每个值运行带注释的测试。
private final static String TEST_DB_JSON = "src\\test\\java\\backend\\database\\testDB.json";
private final static String TEST_DB_YAML = "src\\test\\java\\backend\\database\\testDB.yaml";
private List<byte[]> inputFiles() {
byte[] jsonTestFileState;
byte[] yamlTestFileState;
try {
jsonTestFileState = Files.readAllBytes(Paths.get(TEST_DB_JSON));
yamlTestFileState = Files.readAllBytes(Paths.get(TEST_DB_YAML));
} catch (IOException e) {
throw new IllegalStateException(e);
}
return Arrays.asList(jsonTestFileState, yamlTestFileState);
}
@ParameterizedTest
@MethodSource("inputFiles")
void shouldDoSomething(byte[] testFileState) {
// This method will be called twice: the 1st time with
// jsonTestFileState as the value of the argument
// and the second time with yamlTestFileState
}
• 如何在表格 td 单元格中有两个 <p> 元素不影响单元格的大小?
• 如何将 2d numpy 数组保存到 csv 文件,每个值在不同的单元格中
• 如何将 2d numpy 数组保存到 csv 文件,每个值都在不同的单元格中
• 如何将日期时间字符串格式化为不同文化的 FullDatetimeFormat?
• thera 是一种获得垂直样式的 ag-grid 的方法吗?
• 如何在 Power Query 中找不到数据源时跳过文件的处理?
• PROC SQL 如何连接两个没有空单元格且加上条件的表?
• 我正在尝试绘制图表,但我的所有输出都与下一个单元格重叠,为什么它不腾出额外的空间?
• 在 Python 中将 scad 文件格式转换为 stl
• 如何从 bash 脚本创建 docker-compose 文件
• 如何从不同的 python 文件触发或运行气流 dag。这就像开发一个将运行 dag 并验证其结果的测试用例