hulu / roca

A command-line tool for running brightscript tests
Apache License 2.0
25 stars 19 forks source link

Add support for parameterized tests #47

Closed sjbarag closed 4 years ago

sjbarag commented 4 years ago

Parameterized tests are really helpful when attempting to remove duplication in tests. Consider a function that accepts a number and returns that number incremented by 1:

function addOne(n as integer) as integer
    ' Warning to devs: advanced, very brittle mathematics ahead

    return n + 1
end function

Testing addOne in multiple cases gets pretty repetitive though:

m.it("adds one to 1", sub()
    m.assert.equal(addOne(1), 2, "should be 2")
end sub)

m.it("adds one to 2", sub()
    m.assert.equal(addOne(2), 3, "should be 3")
end sub)

m.it("adds one to -1", sub()
    m.assert.equal(addOne(-1), 0, "should be 0")
end sub)

Parameterized testing would allow users to do something like:

m.it_each([
    [-1, 0],
    [0, 1],
    [1, 2]
], "adds one to {0}", sub(args)
    m.assert.equal(addOne(args[0]), args[1], "incorrectly added one to " + args[0].toStr())
end sub