我试图将我的测试区分为单元测试和集成测试。我的想法是使用新的JUnit5注释@Tag("unit"),它很好地适用于我的JUnit测试,但我无法让它与Spek一起工作。
我现在拥有的是我的班级:
data class MyObject(val value: Int)我的测试:
@Tag("unit")
object MyObjectTest {
@Test
fun checkEquality() {
val o1 = MyObject(1)
assertEquals(o1, o1)
}
}我的build.gradle有:
task utest(type: Test) {
outputs.upToDateWhen { false }
useJUnitPlatform {
includeEngines 'junit-jupiter', 'junit-vintage', 'spek'
includeTags 'unit'
excludeTags 'performance', 'integration', 'functional'
}
testLogging {
events "passed", "skipped", "failed"
}
}当我执行utest时,这是可行的。但是,当对Spek做同样的操作时:
@Tag("unit")
object MyObjectSpek : Spek({
given("an Object") {
val o1 = MyObject(1)
it("should be equal to itself") {
assertEquals(o1, o1)
}
}
})如果我运行分级任务utest,它只执行来自MyObjectTest的方法,而不执行MyObjectSpek的测试
对于如何将Spek与JUnit5标记集成或分离单元测试和集成测试的其他想法,有什么想法吗?
发布于 2018-05-11 20:40:35
今天我遇到了同样的问题。我不得不将测试分为三个部分:单元、服务(测试REST )和集成(WebDriver)。
本指南适用于任何测试框架,而不仅仅适用于Spek。运行此操作需要Gradle 4.6或更高版本。
将独立的测试源集设置为源集
在我的例子中,它们将是:
src/test -用于单元测试(您已经有了)src/serviceTest -用于服务测试src/integrationTest -用于集成测试所有这些集合都应该具有标准的源集结构。在项目中创建这些文件夹,并将包移动到相应的源集。
完成此操作后,请在build.gradle之前添加dependency section,如下行所示:
sourceSets {
integrationTest {
compileClasspath += main.output
runtimeClasspath += main.output
}
serviceTest {
compileClasspath += main.output
runtimeClasspath += main.output
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
serviceTestCompile.extendsFrom testCompile
serviceTestRuntime.extendsFrom testRuntime
}这样做之后,您的IDE (我想您使用了Idea)应该重新索引build.gradle并识别源集。您可能会在新的源集中出现错误,因为它们看不到彼此的源。这是正确的,因为这些源集打算独立运行,不应该是一个问题。
将dependencies 分离到适当的配置(可选)
默认情况下,serviceTest和integrationTest继承所有test依赖项,但是如果需要将特定于某些配置的内容移出公共范围,您可以在这里这样做。
在我的例子中,WebDriver相当重,除了集成测试之外,我不需要它。
dependencies {
// available for all scopes
testCompile "org.jetbrains.spek:spek-api:$spekVersion"
testRuntime "org.jetbrains.spek:spek-junit-platform-engine:$spekVersion"
testCompile "org.junit.platform:junit-platform-launcher:$junitPlatformVersion"
// compiles only for integrationTest
integrationTestCompile "org.seleniumhq.selenium:selenium-java:3.11.0"
integrationTestCompile "org.seleniumhq.selenium.fluent:fluent-selenium:1.19"
}安装程序执行顺序
我们将需要添加测试类型的分级任务并设置它。对于不同的测试任务,可以设置不同的设置。
task serviceTest(type: Test) {
// Runs tests from src/serviceTest
testClassesDirs = sourceSets.serviceTest.output.classesDirs
classpath = sourceSets.serviceTest.runtimeClasspath
}
// Setup serviceTest task
serviceTest {
// Uncomment this if you need to skip tests from the set after first failure. Since Gradle 4.6
//failFast = true
// Enable some logging
testLogging {
events "PASSED", "FAILED", "SKIPPED"
}
// Enable JUnit5 tests
useJUnitPlatform {
}
}对于integrationTest也要这样做。
最后,设置依赖项和执行顺序:
// Make service tests run during gradle check
check.dependsOn serviceTest
check.dependsOn integrationTest
// Make service tests run after unit tests
serviceTest.mustRunAfter test
// Make integration tests run after service tests
integrationTest.mustRunAfter serviceTest结论
你会得到:
Unit -> Service -> Integration测试套件链运行顺序严格;failFast选项如何),那么剩下的测试将不会运行并浪费资源;gradle <task>执行过程中分别从控制台运行每个套件。追加资源:
build.gradle实现此设置的Spek发布于 2018-05-14 08:39:25
在使用IntelliJ时需要考虑的另一件事情是,它在新的源代码集中存在依赖问题,将其添加到build.gradle中:
apply plugin: 'idea'
idea {
module {
testSourceDirs += project.sourceSets.unitTest.kotlin.srcDirs
testSourceDirs += project.sourceSets.unitTest.resources.srcDirs
testSourceDirs += project.sourceSets.integrationTest.kotlin.srcDirs
testSourceDirs += project.sourceSets.integrationTest.resources.srcDirs
testSourceDirs += project.sourceSets.functionalTest.kotlin.srcDirs
testSourceDirs += project.sourceSets.functionalTest.resources.srcDirs
}
}https://stackoverflow.com/questions/50135110
复制相似问题