小编典典

Spring Maven-运行特定的测试(通过注释或Maven配置文件)

java

使用Spring
Maven上下文,我想基于Maven配置文件运行特定的测试。我想有一种标记测试组的简单方法。如果可能的话,我想使用注释。那里有哪些选项,例如maven命令行参数,maven配置文件规范等。

说我有以下测试:

例:

// annotation("integration")
public class GeopointFormatterTest {
    @Test
    public void testIntegration1() {  ...  }   
    @Test
    public void testIntegration2() {  ...  }

当然,@
Profile(用于创建Bean)和@ActiveProfile(用于选择特定的配置文件以创建Bean)之类的注释不能用于选择测试。所有测试仅针对以下语句运行:

mvn clean install-开发

mvn全新安装-Pdevelopment -Dspring.profiles.active = acceptance

mvn全新安装-Pdevelopment -Dspring.profiles.active = integration

根据建议,我还使用了@IfProfileValue。这是根据系统属性值选择测试的好方法。系统属性值可以由CustomProfileValueSource类覆盖,例如:@ProfileValueSourceConfiguration(CustomProfileValueSource.class)

编辑和替代

下面的伟大答案集中于JUnit的@Category机制。谢谢大家!

通过这些步骤,可以采取另一种方法:[1]在maven配置文件中设置一个属性,[2]使用该属性通过标准surefire测试插件的跳过测试。

[1]通过配置文件设置属性:

<profiles>
    <profile>
        <id>integrationtests</id>
        <properties>
            <integration.skip>false</integration.skip>
            <acceptance.skip>true</acceptance.skip>
        </properties>
    </profile>
    ... other profiles

[2]使用surefire测试插件中的属性跳过测试。

<build>
   <plugins>
      <plugin>
         <!-- Run the integration test-->
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>${surefire.plugin.version}</version>
         <configuration>
            <skipTests>${acceptance.skip}</skipTests>

从Maven开始:mvn全新安装–Pintegrationtests


阅读 248

收藏
2020-12-03

共1个答案

小编典典

看一下junit类别

您可以使用特定的类别注释来标记测试

public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }

@Category(SlowTests.class)
public class A {
    @Test public void a() {}
}

然后形成一个像

@RunWith(Categories.class)
@IncludeCategory({FastTests.class})
@SuiteClasses({A.class, B.class})
public static class FastTestSuite {
     //
}

然后用

mvn -Dtest=FastTestSuite test

还要注意,如果您不想在套件类中手动指定单元测试用例类,则还可以使用ClasspathSuite的帮助,然后仅基于类别进行限制。

2020-12-03