【问题标题】:Spockframework soft assertion describes only first failSpockframework 软断言只描述了第一次失败
【发布时间】:2020-08-15 10:07:58
【问题描述】:

Spockframework 提供了软断言机制,但它似乎无法正常工作(至少在我的配置中)。

我创建了最简单的测试:

verifyAll {
    1 == 2
    2 == 3
}

我希望看到两条失败消息,但我只看到第一条:

Condition not satisfied:

1 == 2
  |
  false

第二个也执行了,但是只出现在gradle report中:

org.example.SoftAssertionsTest > simplest test FAILED
    org.spockframework.runtime.SpockComparisonFailure at SoftAssertionsTest.groovy:9
    org.spockframework.runtime.SpockComparisonFailure at SoftAssertionsTest.groovy:10

版本:

compile 'org.codehaus.groovy:groovy-all:2.5.8'
testCompile group: 'junit', name: 'junit', version: '4.12'

testImplementation("org.springframework.boot:spring-boot-starter-test:2.+")
testImplementation('org.spockframework:spock-spring:1.3-groovy-2.5')

我已将此示例推送至:https://github.com/fergus-macdubh/spock-soft-assertions

有没有办法让它显示所有消息?

【问题讨论】:

  • 如果 spock 在第一个错误后继续运行,我会感到非常惊讶。 not 快速失败的用例是什么?如果您想看到许多事情在同一个测试中快速失败,请查看表格和@Unroll 功能。
  • 我无法重现您的问题,对我来说 verifyAll 按预期工作。克隆 Gradle 项目时,我无法构建任何东西,似乎构建文件不完整并且没有定义目标。我是 Maven 人,所以我无法帮助修复您的 Gradle 构建。
  • @cfrick,verifyAll Spock 确实会继续并评估所有条件。这是very purpose of verifyAll

标签: unit-testing testing groovy spock


【解决方案1】:

好的,我在本地安装了旧的 4.4.1 Gradle 版本时看到了类似的行为。但是,当我将gradlew[.bat] 文件添加到您的项目中并将您的构建文件修改为更类似于Spock example project 后,它就起作用了。

apply plugin: "groovy"

group = 'org.example'
version = '1.0-SNAPSHOT'
description = "Soft Assertions"

// Spock works with Java 1.8 and above
sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  // mandatory dependencies for using Spock
  compile "org.codehaus.groovy:groovy-all:2.5.8"
  testCompile platform("org.spockframework:spock-bom:2.0-M1-groovy-2.5")
  testCompile "org.spockframework:spock-core"
  testCompile "org.spockframework:spock-junit4" // you can remove this if your code does not rely on old JUnit 4 rules

  // optional dependencies for using Spock
  testCompile "org.hamcrest:hamcrest-core:1.3" // only necessary if Hamcrest matchers are used
  testRuntime "net.bytebuddy:byte-buddy:1.9.3"          // allows mocking of classes (in addition to interfaces)
  testRuntime "org.objenesis:objenesis:2.6"    // allows mocking of classes without default constructor (together with CGLIB)

  // dependencies used by examples in this project
  testImplementation("org.springframework.boot:spring-boot-starter-test:2.+")
  testImplementation('org.spockframework:spock-spring:1.3-groovy-2.5')
}

test {
  useJUnitPlatform()
}

控制台日志现在看起来像预期的那样:

Testing started at 12:57 ...

> Task :compileJava NO-SOURCE
(...)
> Task :test

Multiple Failures (2 failures)
    org.spockframework.runtime.SpockComparisonFailure: Condition not satisfied:

1 == 2
  |
  false

    org.spockframework.runtime.SpockComparisonFailure: Condition not satisfied:

2 == 3
  |
  false

(...)

【讨论】:

  • 嗨@kriegaex,它适用于提供的配置,谢谢!这是 useJUnitPlatform() spock 2.0 的强制要求吗?我注意到当我将 spock 版本升级到 2 时它总是找不到任何测试(而且useJUnitPlatform() 在实际项目中没有帮助,它仍然找不到任何测试)。
  • 您的示例项目是 Spock 1。顺便说一句,Spock 1 和 2 是不同的,因为 Spock 1 基于 JUnit 4,Spock 2 基于 JUnit 5。而是使用我链接到的项目的主分支如果您需要示例代码,则不要使用 Spock 1.x 分支。还有我未决的pull request 将所有内容升级到最新的 Spock 2 里程碑,它还没有被合并。我建议使用它而不是当前的主人。示例项目包含 Maven 和 Gradle 构建,顺便说一句。我一直使用 Maven,Gradle 根本不是我的菜。
猜你喜欢
  • 2014-07-12
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
相关资源
最近更新 更多