Springboot配置文件和运行一些测试
在springboot应用程序上,我进行了单元测试和集成测试。我想要的是控制运行哪组测试以及何时运行。我的意思是运行单元或集成测试,但不是两者都运行Springboot配置文件和运行一些测试,spring,unit-testing,spring-boot,spring-boot-test,spring-profiles,Spring,Unit Testing,Spring Boot,Spring Boot Test,Spring Profiles,在springboot应用程序上,我进行了单元测试和集成测试。我想要的是控制运行哪组测试以及何时运行。我的意思是运行单元或集成测试,但不是两者都运行 我知道通过maven是可能的,但我想知道是否可以使用Spring Profile来实现这一点。我的意思是,在一个概要文件中标记单元测试,在另一个概要文件中标记集成测试。在运行时,我提供了一个配置文件,它只会触发运行属于该配置文件的测试。是的,您可以按Spring配置文件对这两种测试进行分组。 如果要运行一个或另一个,则必须创建一个特定于单元测试的应
我知道通过maven是可能的,但我想知道是否可以使用Spring Profile来实现这一点。我的意思是,在一个概要文件中标记单元测试,在另一个概要文件中标记集成测试。在运行时,我提供了一个配置文件,它只会触发运行属于该配置文件的测试。是的,您可以按Spring配置文件对这两种测试进行分组。
如果要运行一个或另一个,则必须创建一个特定于单元测试的
应用程序.properties
,以及另一个特定于集成测试的属性。例如
application ut.properties
和application it.properties
及其各自的特性
然后,您应该根据每个测试类的性质为它们指定@ActiveProfiles
对于集成测试类,例如:
@ActiveProfiles("it")
public class FooIntegrationTest {}
@ActiveProfiles("it")
public class BarIntegrationTest {}
@ActiveProfiles("ut")
public class FooTest {}
@ActiveProfiles("ut")
public class BarTest {}
例如,对于单元测试类:
@ActiveProfiles("it")
public class FooIntegrationTest {}
@ActiveProfiles("it")
public class BarIntegrationTest {}
@ActiveProfiles("ut")
public class FooTest {}
@ActiveProfiles("ut")
public class BarTest {}
通过向build.gradle添加以下内容,您可以实现所需的行为:
test {
useJUnitPlatform()
exclude "**/*IT*", "**/*IntTest*"
testLogging {
events 'FAILED', 'SKIPPED'
}
}
task integrationTest(type: Test) {
useJUnitPlatform()
description = "Execute integration tests."
group = "verification"
include "**/*IT*", "**/*IntTest*"
testLogging {
events 'FAILED', 'SKIPPED'
}
}
check.dependsOn integrationTest
这将在验证任务下创建test和integrationTest。现在,通过运行检查任务,您可以运行其中一个或另一个,也可以同时运行这两个。集成测试必须在类名中包含IntTest或它
此外,不要忘记添加以下依赖项:
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.3.2"
testImplementation "junit:junit:4.12"
}
当然,它可以通过
@ActiveProfiles(“profilename”)
注释实现。试试看,我试过了,但没用。我创建了一个名为“e2e”的概要文件和一个名为“application-e2e.properties”的属性文件(实际上是空的)。然后我尝试了“mvn clean install-Dspring.profiles.active=e2e”。但是它运行所有测试。ActiveProfiles仅声明在运行此测试时应激活哪些配置文件。而不是运行此测试所需的激活配置文件。