- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图将我的测试区分为单元测试和集成测试。我的想法是使用新的 JUnit5 注释 @Tag("unit")
,它非常适合我的 JUnit 测试,但我无法让它与 Spek 一起工作。
我目前拥有的是我的类(class):
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)
}
}
})
如果我运行 gradle task utest 会发生什么,它只执行 MyObjectTest
的方法而不执行 MyObjectSpek
关于如何将 Spek 与 JUnit5 标签集成或分离单元测试和集成测试的其他想法有什么想法吗?
最佳答案
今天我遇到了同样的问题。我不得不将测试分为 3 个部分:单元、服务(测试 REST API)和集成(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"
}
设置执行顺序
我们需要添加测试类型的 gradle 任务并进行设置。您可以针对不同的测试任务进行不同的设置。
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
实现这个设置关于java - 为 Spek 使用 JUnit5 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50135110/
Spek 文档引用 As a best practice you typically want test values to be unique for each test this can be d
我检查了文档: https://spekframework.org/migration/#maven-coordinates 我想试用版本 2.x.x,所以我添加了 build.gradle : te
这是我的测试: object MySpek : Spek({ val myMock1: MyMock1 = mock() val myMock2: MyMock2 = mock()
有些测试需要运行数据库,例如,使用测试容器库。启动它显然需要时间。 有没有办法对跨越多个文件的整个 Spek 套件只执行一次此操作?文档对此没有任何说明。 有人知道为什么这还没有实现吗? 最佳答案 这
我正在努力了解 Spek 中的固定装置。 The Calculator example in the docs很容易理解,但我不确定当一些固定装置是有状态的时候如何进行结构化设置/拆卸。例如,如果我正
我正在尝试使用 Spek 测试 Retrofit api 它在 on{...} block 上抛出 nullPointerException 相关堆栈跟踪:https://pastebin.com/g
我正在使用 Kotlin、Spring 和 Spek 实现简单的微服务。我想测试我的存储库,但我想知道如何将 repo 注入(inject) spek 测试用例。每个示例或教程都基于创建这样的新引用:
我写了一个失败的 Spek 测试,因为我对涉及多个协程和 it 的执行顺序做出了错误的假设。职能: given("a test") { runBlocking { print("a") }
我有一个现有的 Java 项目,我希望在其中引入一些规范测试(在 kotlin ofc 中) class CalcSpec: Spek({ given("A calculator") {
我试图将我的测试区分为单元测试和集成测试。我的想法是使用新的 JUnit5 注释 @Tag("unit"),它非常适合我的 JUnit 测试,但我无法让它与 Spek 一起工作。 我目前拥有的是我的类
我正在尝试将 Spek 测试框架添加到我的 Android Studio 项目中。按照说明Here ,我最终将以下内容添加到我的模块 build.gradle: testCompile 'org.je
我想为我的测试使用一个通用的夹具: @RunWith(JUnitPlatform::class) abstract class BaseSpek: Spek({ beforeGroup {pr
对 Kotlin 有点熟悉,我想给它介绍另一个 Android-Java 项目,作为测试的第一步。我决定直接从 Spek 开始. 我在待测模块的build.gradle中添加了如下依赖: testCo
我有一个基于 Gradle 的 Kotlin 项目,其中包含一些 Spek测试,基于 JUnit 并且应该与 Idea 兼容。 但我在上下文菜单中没有看到“运行”菜单项。 为什么?为了能够像其他 JU
严格重现我的问题的 repo :https://github.com/paul-hammant/kotlin-webdriver-snafu 规范,再简单不过了: class WebDriverSpe
spek 尝试查找测试时我遇到了崩溃。我尝试了许多不同的版本和示例配置。我从命令行运行。 Gradle 4.0,mac osx。任何帮助将不胜感激! 这里是错误: WARNING: TestEngin
如何使用 kotlin 启动 spek 测试以测试是否已调用 HTTP 方法 post?让我失望的是我在模拟上下文时遇到了麻烦。我想传入 HttpMethod.POST 以外的方法来触发 else b
我很难配置基于 JUnit Jupiter 的测试环境 (5)。我在那里有两个不同的错误: WARNING: TestEngine with ID 'spek' failed to discover
我是在 kotlin 测试中使用 spek 的新手。使用 spek 时,在 logcat 上出现以下错误。我不明白我为什么得到这个 java.lang.ClassNotFoundException:
我是一名优秀的程序员,十分优秀!