英文:
How to use JUnit5 Parametrized Test CSV file source?
问题
我正在尝试从CSV文件运行参数化测试。
如果我只使用CSVSource,就像这样:
@ParameterizedTest
@CsvSource({ "a,A", "b,B" })
void csvSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
但是,如果我尝试从文件中进行相同的操作,它不起作用:
@ParameterizedTest
@CsvFileSource(resources = "/data.csv")
void csvFileSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
我还尝试过使用文件的硬路径,但是对于Eclipse中的文件测试,我总是收到消息:
找不到使用测试运行器 'JUnit 5' 进行的测试。
JUnit期望文件在哪里?
这是我的依赖项:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
有人知道我可能漏掉了什么或错误在哪里吗?
提前感谢。
PAUL
英文:
I am trying to run a parametrized test from CSV-file.
It is working if I use just CSVSource like that:
@ParameterizedTest
@CsvSource({ "a,A", "b,B" })
void csvSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
But if I try the same thing from a file it won't work:
@ParameterizedTest
@CsvFileSource(resources = "/data.csv")
void csvFileSourceTest(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
I have also tried using a hard path to my file but for the file test in Eclipse I always get the message
> No tests found with test runnter 'JUnit 5'.
Where does JUnit expect the file?
These are my dependencies:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
Does anybody know what I might be missing or where the error is?
Thanks in advance <br>
PAUL
答案1
得分: 1
JUnit5 用户指南 指出 CSV 文件需要位于类路径上。对于 Maven 项目来说,这将是 src/test/resources
。
英文:
The JUnit5 user guide states that the CSV file needs to be on the classpath. In case of a Maven project that would be src/test/resources
.
答案2
得分: 0
在Kotlin中:
CSV文件位于以下位置:
/src/test/resources/post_data.csv
测试读取CSV测试数据:
@ParameterizedTest(name = "{index} => post with {0} : {1} : {2}")
@CsvFileSource(resources = ["/post_data.csv"])
fun testPostWithValidData(id: String?, text: String?, completed: String?) {
// 测试代码
// ......
}
Gradle 依赖项:
// JUnit5
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-params:$junitVersion")
英文:
In Kotlin
CSV file is placed here:
> /src/test/resources/post_data.csv
Test read csv test data:
@ParameterizedTest(name = "{index} => post with {0} : {1} : {2}")
@CsvFileSource(resources = ["/post_data.csv"],)
fun testPostWithValidData(id: String?, text: String?, completed: String?) {
//test body
......
}
Gradle dependencies:
> // JUnit5
> testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
> testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
> testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
> testRuntimeOnly("org.junit.jupiter:junit-jupiter-params:$junitVersion")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论