简体   繁体   English

如何使用JUnit5参数化测试CSV文件源?

[英]How to use JUnit5 Parametrized Test CSV file source?

I am trying to run a parametrized test from CSV-file.我正在尝试从 CSV 文件运行参数化测试。

It is working if I use just CSVSource like that:如果我像这样只使用 CSVSource,它就可以工作:

@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我也尝试过使用我的文件的硬路径但是对于 Eclipse 中的文件测试我总是收到消息

No tests found with test runnter 'JUnit 5'.未找到测试运行程序“JUnit 5”的测试。

Where does JUnit expect the file? JUnit 在哪里期望文件?

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提前致谢
PAUL保罗

The JUnit5 user guide states that the CSV file needs to be on the classpath. JUnit5 用户指南指出 CSV 文件需要在类路径上。 In case of a Maven project that would be src/test/resources .如果 Maven 项目是src/test/resources

In Kotlin Kotlin

CSV file is placed here: CSV 文件放在这里:

/src/test/resources/post_data.csv /src/test/resources/post_data.csv

Test read csv test data:测试读取csv测试数据:

        @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: 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") // 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM