栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Spock-Genesis】迭代规则变更——@Iterations、 Once、*、Take

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spock-Genesis】迭代规则变更——@Iterations、 Once、*、Take

文章目录
  • 前言
  • 一、注解@Iterations
  • 二、只取给定值一次——Once
  • 三、multiply by通过乘号*
  • 四、从数据集中选择想要的数量—take
  • 总结


前言

数据生成器可以根据数据源分为有限和无限两个类别。实际测试中我们都是需要有限的数据进行测试,因此需要我们通过多种手段改变生成数量的规则。


一、注解@Iterations

如果希望限制运行的迭代次数,注解@Iterations是一个不错的选择。只要给指定值即可按照需求运行次数,如果不指定运行次数,默认运行100次。

    static List NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

    @Iterations(3)
    def 'method iterations is limited by class annotation'() {
        expect:
        value < 4
        println(value)
        where:
        value << NUMBERS
    }

运行结果:

1
2
3
只运行了3次,参数条件从数组中按照顺序取得三个。

同样如果给定的数据源可以无限,那我们也可以指定运行次数

    @Iterations(5)
    def 'limiting iterations to 5 makes it so the first 2 iterations are all that run'() {
        expect:
        println(s)
        println(i)

        where:
        s << Gen.string(~/[A-Z][a-z]+( [A-Z][a-z]+)?/)
        i << Gen.these(1, 2, 3, 4, 5, 6)
    }

运行结果:

Mbq Brpxw
1
Qbv Husknmzswj
2
Dd Vqpski
3
Vko
4
Sfv El
5

二、只取给定值一次——once
    def 'generate a value once'() {
        setup:
        def gen = Gen.once value
        expect:
        gen.collect() == [value]
        println(value)
        where:
        value << [null, 1, 1,'b', [1,2]]
    }

运行结果:

null
17:56:22.165 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - After iteration: generate a value once
17:56:22.169 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - Before iteration: generate a value once
1
17:56:22.170 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - After iteration: generate a value once
17:56:22.173 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - Before iteration: generate a value once
1
17:56:22.173 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - After iteration: generate a value once
17:56:22.175 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - Before iteration: generate a value once
b
17:56:22.175 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - After iteration: generate a value once
17:56:22.177 [main] DEBUG com.athaydes.spockframework.report.SpecInfoListener - Before iteration: generate a value once
[1, 2]
运行记录中会有只引用一次的记过记录,但是和我们平时普通的运行一致。并没有差别,也许特殊场景才能发挥其真实的作用

三、multiply by通过乘号*
   def 'multiply by int limits the quantity generated'() {
        setup:
        def gen = Gen.string(5) * 3
        when:
        def results = gen.collect()
        then:
        results.size() == 3
        println(results)
    }

运行结果:

[3rvFX, 0BB, AgO]
会有三个输入结果

四、从数据集中选择想要的数量—take
   def 'generate a value repeatedly'() {

        expect:
        gen.size() == 10
        println(gen)

        where:
        gen = Gen.integer(3..4).iterator().take(10).collect()

    }

运行结果:

[4, 3, 3, 4, 4, 4, 3, 4, 3, 4]


总结

该片文章主要介绍了一些巧妙的生成规则方法的使用,对于一些数据数量的处理很是方便。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号