Search code examples
testinggroovybddeasyb

EasyB testing multiple input/ouput values


How do I test a functionality with multiple inputs/expected outputs?

Here is a really simple example:

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
    }
}

I want to test this with multiple values, say add(4,8).shouldBe 12, ....

Whats the best practice to do this? In other BDD frameworks I have seen table like structures to implement this, but cannot find something like that in EasyB. Should I create multiple scenarios to cover this (appending (1), (2) to the scenario name), or should I put the inputs and expected outputs into an array, and check this for equality? If I use the latter approach, how do I get meaningfull failures?


Solution

  • Use the where/example clause 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
      }
    }