Spring Maven-运行特定的测试(通过注释或Maven配置文件) [英] Spring maven - run specific tests (via annotations or maven profile)

查看:227
本文介绍了Spring Maven-运行特定的测试(通过注释或Maven配置文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using a Spring maven context, I would like to run specific tests based on a maven profile. I would like to have an easy way of tagging the test groups. If possible I would like to use the annotations. Which options are there, like maven command line parameters, maven profiles specification, etc.

说我有以下测试:

示例:

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

当然,不能使用诸如@Profile(用于创建bean)和@ActiveProfile(用于选择用于创建bean的特定概要文件)之类的注释.所有测试仅针对以下语句运行:

Annotations like @Profile (which is for creating beans) and @ActiveProfile (which is for selecting specific profiles for creating beans) cannot be used for selecting tests, of course. All tests just run for statements like:

mvn全新安装-Pdevelopment

mvn clean install -Pdevelopment

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

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

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

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

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

As suggested, I used also @IfProfileValue. This is a good way for selecting tests based on system property values. System property values can be overruled by a CustomProfileValueSource class, like in: @ProfileValueSourceConfiguration(CustomProfileValueSource.class)

编辑和替代

下面的GREAT答案集中在JUnit的@Category机制上.谢谢大家!

The GREAT answers below focus on JUnit's @Category mechanism. Thanks to all!

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

A different approach is via these steps: [1] set a property within a maven profile and [2] use the property to skip tests via the of the standard surefire test plugin.

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

[1] Setting the properties via a profile:

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

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

[2] Using the properties in the surefire test plugin to skip tests.

<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

Start in maven: mvn clean install –Pintegrationtests

推荐答案

看看 junit类别.

您要使用特定的类别注释标记测试

You'd tag your tests with specific category annotations

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

还请注意,如果您不想在套件类中手动指定单元测试用例类,则还可以使用

Note also that if you don't want to manually specify your unit test case classes in the suite class, you can also use the help of ClasspathSuite and then just limit based on categories.

这篇关于Spring Maven-运行特定的测试(通过注释或Maven配置文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆