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

huangapple go评论76阅读模式
英文:

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({ &quot;a,A&quot;, &quot;b,B&quot; })
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 = &quot;/data.csv&quot;)
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:

&lt;dependency&gt;
	&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
	&lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
	&lt;version&gt;5.1.0&lt;/version&gt;
	&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
	&lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
	&lt;version&gt;5.1.0&lt;/version&gt;
	&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

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 = &quot;{index} =&gt; post with {0} : {1} : {2}&quot;)
        @CsvFileSource(resources = [&quot;/post_data.csv&quot;],)
        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")

huangapple
  • 本文由 发表于 2020年9月15日 18:03:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63899534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定