使用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 = accepted
mvn clean install -Pdevelopment -Dspring.profiles.active = integration
正如所建议的那样,我也使用了@IfProfileValue。这是根据系统属性值选择测试的好方法。 CustomProfileValueSource类可以推翻系统属性值,如:@ProfileValueSourceConfiguration(CustomProfileValueSource.class)
编辑和替代
下面的GREAT答案主要关注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 clean install -Pintegrationtests
看一眼 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 然后根据类别进行限制。
我们在以下步骤中解决了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)运行命令as mvn test -PreleaseTest
您可能需要使用以下内容对测试进行分类 @Category
注解。该中提供了一个完整的例子 Surefire
提供的文件 这里 - 搜索字符串 使用JUnit类别。
假设您已相应地对测试进行了分类,您现在可以在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
您可以使用此标志指定配置文件:
mvn test -Dspring.profiles.active=acceptance
在我的最新项目中,我有一个“集成”配置文件,用于对嵌入式H2数据库运行集成测试。
根据一些帖子的答案,我创建了一个 简单的测试项目 它展示了许多代码质量和测试功能:
- 使用插件执行单元测试或集成测试 万全 和 故障安全。
- 通过插件提高代码质量 FindBugs的。
- 使用插件确定测试覆盖率统计信息 Jacoco。
请享用!