在Spock中编写一种参数化的测试方法



我正在用spock编写测试,目前,这是基本结构:

def "someTest"(String str, Class<? extends SomeClass> clazz) {
    setup:
       (current implementation)
       obj.get("Sample1")
       obj.get("Sample2")
       obj.get("Sample3")
       ... so on
       (what I want to implement)
       object.get(str)
    when:
    ...
    then:
    ...
}

我需要在setup:when:then:中使用strclazz,因此我需要多次调用该方法的方法。

我已经在线阅读了一些tuts:https://www.testwithspring.com/lesson/writing-parameterized-parameterized-tests-with-spock-framework/,但真的不知道如何使用非启示性实现它类型

在spock中,您在where:部分中提供参数化测试的值,作为ASCII'表'或列表。Spock使用AST转换将该表转换为单独的方法调用。所以看起来像魔术。

  @Unroll // formats method name. Can be on class level
  def "Name of #clazz is not #str"(String str, Class clazz) {
    setup:
    // TODO: real setup
    str == clazz.name
    // TODO: when/ then only useful for stimulus-response tests
    when:
    str == clazz.name
    then:
    str == clazz.name
    // TODO: expect not needed when using when/then
    expect:
    str == clazz.name
    where:
    str                  | clazz
    "java.lang.String"   | String.class
    "java.lang.Integer"  | Integer.class
  }

这可能看起来不像它,但是该方法在"表"的"表"中多次运行,您可以使用无效的值检查并使每个方法调用失败。

相关内容

最新更新