Spock:如何使用@Stepwise

时间:2016-08-30 07:20:18

标签: loops testing spock geb

我使用@Stepwise进行自动化。 8个测试方法按顺序运行以完成该过程。其中一个方法采用参数,我想将不同的参数传递给方法。

但问题是:该方法采用第一组参数,然后参数处理 AND 而不是进入下一个方法,该方法采用下一组参数。

来源如下:

@Stepwise
class AOSearchPageTest extends GebReportingSpec
{
    def "first_method"()
    {
    }

    def "second_method"()
    {
        when:
        at particularPage
        then:
        process(area)
        process(coutntry)
        process(airport)
        process(dest_area)
        process(dest_coutntry)
        process(dest_airport)

        where:
        area << ["asia","europe"]
        country << ["japan","uk"]
        port << ["narita","london"]
        dest_area << ["asia","europe"]
        dest_country << ["korea","italy"]
        dest_port << ["seul","florence"]
    }

    def "third_method"()
    {
        //Some other processing
    }

第二种方法首先填充“亚洲”,“日本”,“成田”,“亚洲”,“韩国”,“seul”

而不是执行“third_method”(), 它需要第二组参数 欧洲,英国,伦敦,欧洲,意大利,佛罗伦萨。

我如何处理每组数据,以便为每组数据从上到下执行所有方法[defs]?

2 个答案:

答案 0 :(得分:0)

首先,@Stepwise

Indicates that a spec's feature methods should be run sequentially in their declared order (even in the presence of a parallel spec runner), always starting from the first method. If a method fails, the remaining methods will be skipped. Feature methods declared in super- and subspecs are not affected.

where子句表示您的方法执行次数。你无法完成它的完整执行,无法从一种方法转到另一种方法。 因此,您可以将您的dependents任务移动到一个方法中,并在您的帮助器方法中移动一些处理以减少spec方法中的代码行。

答案 1 :(得分:0)

使third_method成为未作为测试执行的私有独立方法,并在second_method中的处理块之后调用它。像这样:

@Stepwise
class AOSearchPageTest extends GebReportingSpec
{
    def "first_method"()
    {
    }

    def "second_method"()
    {
        when:
        at particularPage

        then:
        process(area)
        process(coutntry)
        process(airport)
        process(dest_area)
        process(dest_coutntry)
        process(dest_airport)

        and:
        thirdMethod()

        where:
        area << ["asia","europe"]
        country << ["japan","uk"]
        port << ["narita","london"]
        dest_area << ["asia","europe"]
        dest_country << ["korea","italy"]
        dest_port << ["seul","florence"]
    }

    private def thirdMethod()
    {
        //Some other processing
    }

但请注意,在这种情况下,thirdMethod需要返回一个布尔结果,表示之前third_method测试成功或失败。