@parameterizedTest不使用参数
我正在为Azure功能创建测试用例。我想使用参数化测试,但它拒绝选择任何具有参数的测试。我使用官方的Azure函数Maven原型来生成项目。这是POM的相关部分(注意:我放置了排除,以确保我不会在Junit4中偶然加载(没有更改任何内容),并且我的POM中没有特定的Surefire-Plugin配置部分)。
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>1.18.0</version>
<configuration>
<appName>${functionAppName}</appName>
<resourceGroup>example</resourceGroup>
<appServicePlanName>example</appServicePlanName>
<region>example</region>
<!-- <pricingTier></pricingTier> -->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>linux</os>
<javaVersion>11</javaVersion>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~4</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
这是测试代码:
public class FunctionTest {
@Test
public void testTrue() {
assertTrue(true);
}
@ParameterizedTest
@ValueSource(strings = {"example"})
public void testNoParams() {
assertTrue(true);
}
@ParameterizedTest
@ValueSource(strings = {"example"})
public void testOneParam(String string) {
assertEquals(string, "example");
}
@ParameterizedTest
@MethodSource
public void testExtractSiteKeyShouldReturnMatchingGroup(String input, String output) throws Exception {
Function function = new Function();
assertEquals(function.extractSiteKey(Logger.getGlobal(), input), output);
}
private static Stream<Arguments> testExtractSiteKeyShouldReturnMatchingGroup() {
return Stream.of(
Arguments.of(null, null),
Arguments.of("", null),
Arguments.of(" ", null),
Arguments.of("notmatching", null),
);
}
}
仅次的两个测试是没有参数的两个测试:
<testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testNoParams" time="0.001"/>
<testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testTrue" time="0"/>
作为一些其他信息,它还拒绝拿起未与“测试”前缀的测试用例,例如TestoneParam作品,OneParam不做。
I'm creating test cases for an Azure Function. I'd like to use parameterized tests but it refuses to pick up any test that has parameters. I used the official Azure Functions Maven archetype to generate the project. Here's the relevant parts from the POM (NOTE: I put in the exclusion to make sure I wasn't accidentally loading in junit4 (didn't change anything), and there is no specific surefire-plugin configuration section in my POM).
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>1.18.0</version>
<configuration>
<appName>${functionAppName}</appName>
<resourceGroup>example</resourceGroup>
<appServicePlanName>example</appServicePlanName>
<region>example</region>
<!-- <pricingTier></pricingTier> -->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>linux</os>
<javaVersion>11</javaVersion>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~4</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
and here's the test code:
public class FunctionTest {
@Test
public void testTrue() {
assertTrue(true);
}
@ParameterizedTest
@ValueSource(strings = {"example"})
public void testNoParams() {
assertTrue(true);
}
@ParameterizedTest
@ValueSource(strings = {"example"})
public void testOneParam(String string) {
assertEquals(string, "example");
}
@ParameterizedTest
@MethodSource
public void testExtractSiteKeyShouldReturnMatchingGroup(String input, String output) throws Exception {
Function function = new Function();
assertEquals(function.extractSiteKey(Logger.getGlobal(), input), output);
}
private static Stream<Arguments> testExtractSiteKeyShouldReturnMatchingGroup() {
return Stream.of(
Arguments.of(null, null),
Arguments.of("", null),
Arguments.of(" ", null),
Arguments.of("notmatching", null),
);
}
}
The only two tests that are picked up are, which are the two that don't have parameters:
<testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testNoParams" time="0.001"/>
<testcase classname="com.example.FunctionTest" name="com.example.FunctionTest.testTrue" time="0"/>
As some additional information, it also refuses to pick up the test cases that aren't prefixed with "test", e.g. testOneParam works, oneParam doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题是通过在2.22.2上方的Maven-Surefire插件版本中明确添加来解决的:
开箱即用的版本是2.12.4版(显然它仅在Junit 5.5.1和Surefire-Plugin起就可以拾取@parametertest注释版本2.22.2)
The issue was fixed by explicitly adding in the maven-surefire plugin version above 2.22.2:
The one provided out of the box is version 2.12.4 (apparently it only picks up @ParameterTest annotations as of junit 5.5.1 and surefire-plugin version 2.22.2)