小编Gas*_*ton的帖子

表测试多返回值函数

我在Go上切齿,在深入研究表驱动测试后,我遇到了以下问题:

我有一个返回多个值的函数

// Halves an integer and and returns true if it was even or false if it was odd.
func half(n int) (int, bool) {
    h := n / 2
    e := n%2 == 0
    return h, e
}
Run Code Online (Sandbox Code Playgroud)

我知道,对于half(1)返回值应该是0, falsehalf(2)它应该匹配1, true,但我似乎无法弄清楚如何将它放在一个表上.

怎么会有类似下面的东西?

var halfTests = []struct {
    in  int
    out string
}{
    {1, <0, false>},
    {3, <1, true>},
}
Run Code Online (Sandbox Code Playgroud)

这样做还有其他更惯用的方法吗?

作为参考,这里使用表来测试类似于FizzBu​​zz函数的东西:

var fizzbuzzTests = []struct {
    in  int …
Run Code Online (Sandbox Code Playgroud)

testing unit-testing go

3
推荐指数
1
解决办法
1735
查看次数

标签 统计

go ×1

testing ×1

unit-testing ×1