首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EasyB测试多个输入/输出值

EasyB测试多个输入/输出值
EN

Stack Overflow用户
提问于 2012-07-06 16:58:00
回答 1查看 294关注 0票数 1

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

下面是一个非常简单的例子:

代码语言:javascript
复制
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)追加到场景名称中),还是应该将输入和预期输出放入一个数组中,并检查是否相等?如果我使用后一种方法,我如何获得有意义的失败?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-11 14:44:05

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

代码语言:javascript
复制
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
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11359045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档