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

26

使用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 -Pdevelopment

mvn clean install -Pdevelopment -Dspring.profiles.active=acceptance

mvn clean install -Pdevelopment -Dspring.profiles.active=integration

如建议的那样,我还使用了 @IfProfileValue。这是一种基于系统属性值选择测试的好方法。可以通过 CustomProfileValueSource 类覆盖系统属性值,例如:@ProfileValueSourceConfiguration(CustomProfileValueSource.class)

编辑和替代方案

下面的出色答案集中于 JUnit 的 @Category 机制。感谢所有人!

另一种方法是通过以下步骤进行:[1] 在 maven 配置文件中设置属性,[2] 使用该属性通过标准 surefire 测试插件中的 <excludeGroups> 跳过测试。

[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 clean install –Pintegrationtests


如果您的问题得到解答,请考虑接受答案或提供自己的答案并接受它。此问题仍标记为开放状态并具有悬赏。 - eis
另外,您添加的答案似乎有些不完整 - 目前它只是基于Maven配置文件运行Surefire插件,但是如果没有更多的配置,它将始终仅运行标准单元测试。 - eis
5个回答

24

请查看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的帮助,然后仅基于类别进行限制。

谢谢。这也是一个非常好的选项。你能在没有测试套件的情况下做到这一点吗? 如果您不指定-D参数,它会跳过具有类别的测试吗? - tm1701
@tjm1706 如果您没有指定,则会运行所有测试。是的,您可以在没有测试套件的情况下完成此操作,例如按照此其他答案中指定的方式。 - eis

7
你可能需要使用@Category注解对测试进行分类。在Surefire提供的文档中,已经提供了一个完整的示例,链接地址为here,搜索字符串Using JUnit Categories即可找到。
假设你已经按照分类对测试进行了分类,现在你可以在Maven构建中设置一个或多个配置文件,以根据测试类别触发这些测试。
<profiles>
    <profile>
        <id>slow-tests</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.11</version>
                    <configuration>
                        <groups>com.mycompany.SlowTests</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>fast-tests</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.11</version>
                    <configuration>
                        <groups>com.mycompany.FastTests</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

在运行测试时,您可以在命令行上指定一个或多个配置文件。

mvn test -Pslow-tests,fast-tests

谢谢。如果您指定不使用配置文件,是否有一种方法只运行没有类别的测试? - tm1701
1
您可以创建一个默认处于活动状态的配置文件,并排除您不希望运行的组。这里有更多信息。https://dev59.com/BGYq5IYBdhLWcg3w30Xc - Srikanth Anusuri
我认为最好的做法是为那些你想要在没有-P开关的情况下运行的测试创建一个类别,将这些测试方法捆绑在一个测试类中,并用你创建的新类别对该测试类进行注释。 然后在你的POM中,为新的类别组指定一个配置文件,并默认激活该配置文件。 因此,当你下一次执行"mvn test"时,只有这些测试方法会运行。 - Christian Oliver

4
你可以使用这个标志指定配置文件:
mvn test -Dspring.profiles.active=acceptance

在我的最新项目中,我有一个“集成”配置文件,用于针对嵌入式H2数据库运行集成测试。


1
我们已经按照以下步骤解决了 JUnit 的分类问题。
我已在 GitHub 上为您创建了一个项目。 https://github.com/djaganathan/unit-test-samples 注意:JUnit 分类包仍然处于实验阶段。
1)创建了一个接口分类。


    /**
     * This interface used to categories Junit Test
     * those Tests will be executed during bamboo build run
     */
    public interface ReleaseTest {
    }

2) 将单元测试用例标记为所需的类别



    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.junit.internal.runners.JUnit4ClassRunner;
    import org.junit.runner.RunWith;

    import com.github.djaganathan.unit.test.category.ReleaseTest;

    @RunWith(JUnit4ClassRunner.class)
    public class GeneralTest {

        @Test
        @Category(value={ReleaseTest.class})
        public void doTestForRelease(){}

        @Test
        public void doTestForDev(){}

    }

3) 在Maven中创建配置文件并将其附加到项目中

4) 运行命令 mvn test -PreleaseTest


谢谢。如果您指定不使用配置文件,是否有一种方法只运行没有类别的测试? - tm1701

1

根据一些帖子的答案,我创建了一个简单测试项目,演示了许多代码质量和测试功能:

  • 使用插件SurefireFailsafe执行单元测试或集成测试。
  • 通过插件findbugs提高代码质量。
  • 使用插件Jacoco确定测试覆盖率统计数据。

享受吧!


嗨!我刚刚浏览了你的Github项目。我不明白的是,Maven如何知道它必须运行用@Category(IntegrationTest.class)注释的测试。但是,给定的配置文件是-PintegrationintegrationIntegrationTest之间的映射在哪里?难道它们不应该是相同的吗? - Arun Gowda
1
这是一段时间之前的事了。在 pom.xml 文件中,你会看到 <groups>IntegrationTest</groups>。 - tm1701

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接