java - JUnit 5 参数化测试:将 CSVFileSource 与 Enclosed.Class 一起使用
问题描述
我正在尝试使用在同一类中运行参数化和非参数化测试用例
@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)
但不知何故,测试没有运行。我试过csvFileSource
没有Enclosed
课,效果很好。这是我的测试类骨架的样子:(请帮助)
@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)
public class MyTest {
static class Base{
}
@RunWith(Parameterized.class)
public static class ParameterizedTests extends Base {
@ParameterizedTest(name = "testString:{0}")
@CsvFileSource(resources = "testCases.csv")
public void test(String testString) {
....
}
}
}
解决方案
首先,@RunWith
是来自 JUnit 4 的注释。运行 JUnit Jupiter 时不需要它@ParameterizedTest
。有关详细信息,请参阅https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests。
接下来,只有非静态嵌套类(即内部类)可以作为@Nested
测试类。有关详细信息和推理,请参阅https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested。
最小的工作示例:
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class MyTest {
class Base {}
@Nested
class ParameterizedTests extends Base {
@ParameterizedTest(name = "testString:{0}")
@ValueSource(strings = "testCases.csv")
void test(String testString) {
System.out.println(testString);
}
}
}
推荐阅读
- python - 找不到硒元素
- unity3d - 尝试在 WebGL 中加载可寻址对象时出错
- python - Tkinter 使用鼠标调整画布上的矩形大小
- python - 在 3 个不同的分类器上使用相同的数据集会输出相同的混淆矩阵/准确度分数
- php - 获取用户 ID 并将其分配给项目表行 2x 表
- spring - 按日期范围分组的 Spring 数据 Elasticsearch 计数
- excel - 如何分解一个非常大的excel文件
- javascript - 尝试将数据快照放入我想要的类时出现问题
- django - 如何在 Django 中将 UTC 时间转换为用户的本地时间
- python - 我需要使用变量调用 python 函数