2017-10-10 2 views
4

이렇게 테스트를하고 where 절 데이터 테이블을 재사용 가능한 블록으로 추출 할 수 있습니까? 이 (의사 코드) 같은Spock : 재사용 가능한 데이터 테이블

@Unroll 
void "test that doSomething with #a and #b does not fail"(String a, String b) { 
    when: 
     doSomethingWithAandB(a, b) 
    then: 
     notThrown(Exception) 
    where: 
     a  | b 
     "foo" | "bar" 
     "foo" | "baz" 
     "foo" | "foo" 
} 

뭔가 : 예, 당신이 할 수있는

@Unroll 
void "test that doSomethingElse with #a and #b does not fail"(String a, String b) { 
    when: 
     doSomethingElseWithAandB(a, b) 
    then: 
     notThrown(Exception) 
    where: 
     dataTable() 
} 

def dataTable(a, b) { // this is now reusable in multiple tests 
     a  | b 
     "foo" | "bar" 
     "foo" | "baz" 
     "foo" | "foo"   
} 
+0

다음 페이지를 참조하십시오. https://stackoverflow.com/questions/26156544/using-spock-data-table-for-filling-objects – Opal

답변

1

. 물론

import spock.lang.Specification 
import spock.lang.Unroll 

class SampleTest extends Specification { 
    @Unroll 
    def "max of #a and #b gives #c"() { 
    expect: 
    Math.max(a, b) == c 
    where: 
    a << aProvider() 
    b << bProvider() 
    c << cProvider() 
} 

private List<Integer> aProvider() { 
    [1 ,2 ,4] 
} 
private List<Integer> bProvider() { 
    [0 ,2 ,5] 
} 

private List<Integer> cProvider() { 
    [1 ,2 ,5] 
} 
} 

는 aProvider/bProvider/cProvider 어떤 클래스에 외부화 많은 테스트에서 재사용 될 수있다,는 'groovier 방법'과 다른 것들 사이에 다시 쓸 수 있습니다. 테이블을 지정할 필요는 없지만 '데이터 파이프'를 제공 할 수 있습니다. Data Driven Testing 장을 읽어보십시오.

0

감사합니다. answer에게 감사합니다. 내가 배운 것을 바탕으로 합리적으로 간단한 해결책을 찾았습니다. where 절에

import spock.lang.Specification 
import spock.lang.Unroll 

class SampleTest extends Specification { 
    @Unroll 
    def "max of #a and #b gives #c"() { 
     expect: 
      Math.max(a, b) == c 
     where: 
      params << dataTable() 
      a = params.a 
      b = params.b 
      c = params.c 
    } 

    // this is now reusable in multiple tests 
    def dataTable() { 
     return [ 
      // case 1 
      [ 
       a: 1, 
       b: 0, 
       c: 1 
      ], 

      // case 2 
      [ 
       a: 1, 
       b: 2, 
       c: 2 
      ], 

      // case 3 
      [ 
       a: 4, 
       b: 5, 
       c: 5 
      ] 
     ] 
    } 
} 
1

표 형식의 데이터를 실제로 컴파일 타임에 OR 표현, 목록의 목록에 수집 한 후 전치의 집합으로 해석됩니다, 그래서 이것은 :

where: 
a | b | c 
1 | 0 | 1 
2 | 2 | 2 
4 | 5 | 5 

로 변환됩니다 이 방법은 시험 방법을 만들기 전에

입니다 (자세한 내용은 org.spockframework.compiler.WhereBlockRewriter 참조). 이 var << list 구조는 documentation에서 "데이터 파이프"라고합니다.

class SampleTest extends Specification { 
    @Unroll 
    def "max of #a and #b gives #c"() { 
     expect: 
     Math.max(a, b) == c 
     where: 
     [a, b, c] << dataTable() 
    } 

    static def dataTable() { 
     [ 
       [1, 0, 1], 
       [2, 2, 2], 
       [4, 5, 5] 
     ] 
    } 
} 

:

조금, 스팍 1.1로, 하나는 테이블 조옮김합니다 "다중 변수 데이터 파이프"라는 구조를 사용하여 코드를 조금 줄일 수 있습니다 기존의 답변을 업그레이드

재미있는 사실은 다음 docs on Syntactic Variations이 이유를 설명하지 않는 상태에서 테이블 행이 OR 표현의 집합으로 해석되기 때문에, 그것이, 이중 막대도 사용할 수 있습니다 -

where: 
a | b || c 
1 | 0 || 1 
2 | 2 || 2 
4 | 5 || 5 
관련 문제