【问题标题】:EasyB testing multiple input/ouput values轻松测试多个输入/输出值
【发布时间】:2012-07-06 17:25:37
【问题描述】:

如何测试具有多个输入/预期输出的功能?

这是一个非常简单的例子:

scenario "Can add two numbers", {
    given "Two numbers", {
        num1 = 2
        num2 = 3
    }

    when "I trigger add.", {
        result = add(num1,num2)
    }

    then "The result should be correct.", {
        result.shouldBe 5
    }
}

我想用多个值来测试这个,比如add(4,8).shouldBe 12, ....

这样做的最佳做法是什么?在其他 BDD 框架中,我看到了类似表的结构来实现这一点,但在 EasyB 中找不到类似的东西。我应该创建多个场景来涵盖这一点(将 (1)、(2) 附加到场景名称),还是应该将输入和预期输出放入一个数组中,并检查它是否相等? 如果我使用后一种方法,我如何获得有意义的失败?

【问题讨论】:

    标签: testing groovy bdd easyb


    【解决方案1】:

    使用 where/example 子句 http://code.google.com/p/easyb/wiki/ChangesInEasyb098

    package org.easyb.where
    
    /*
    Example tests a map at the story level
     */
    
    numberArray = [12, 8, 20, 199]
    
    where "we are using sample data at a global level", [number:numberArray]
    
    before "Before we start running the examples", {
      given "an initial value for counters", {
        println "initial"
        whenCount = 0
        thenCount = 0
        numberTotal = 0
      }
    }
    
    scenario "Number is #number and multiplier is #multiplier and total is #{number * multiplier}", {
      when "we multiply #number by #multiplier", {
        whenCount ++
        num = number * multiplier
      }
      then "our calculation (#num) should equal #{number * multiplier}", {
        num.shouldBeGreaterThan 0
        numberTotal += num
        thenCount ++
      }
      where "Multipliers should be", {
        multiplier = [1,2,3]
      }
    }
    
    
    after "should be true after running example data", {
      then "we should have set totals", {
        whenCount.shouldBe 12
        thenCount.shouldBe 12
        num = 0
        numberArray.each { n ->
          num = num + (n + (2*n) + (3*n))
        }
    
        num.shouldBe numberTotal
      }
    }
    

    【讨论】:

    • 感谢您推荐发行说明。它在手册中丢失。但我认为它的编码和阅读有点困难,然后是“表驱动”(如黄瓜)。
    • 确实如此,我认为这是以本示例的方式压缩故事和步骤的成本。它更像是黄瓜/rspec 组合,而不是单独的黄瓜。
    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多