我可以迭代运行ginkgo的测试吗?

huangapple go评论77阅读模式
英文:

Can I Iterate the tests for ginkgo

问题

是否可以拥有可变数量的测试用例?假设我有一个BeforeSuite函数,用于计算数组中的值。

然后根据数组的长度,我想每个数组元素运行一个测试用例。

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // 每次运行套件时可以变化
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
            It("multiple tests", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
        }
    })
})

我了解Ginkgo的运行方式。它进行两次解析。首先运行所有非规范元素,然后运行规范元素。

在GitHub上有一个问题https://github.com/onsi/ginkgo/issues/462,它适用于我的用例,但原帖的作者误解了,并采取了不同的方向。

英文:

Is it possible to have a variable number of test cases. Let's say I have a BeforeSuite function which calculates the values in the array.

Then based on the length of the array, I want to run one test per array element.

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // can vary every time i run the suite
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
            It("multiple tests", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
        }
    })
})

I do understand the way Ginkgo runs. It does 2 parses. First it run all the non-spec elements and then runs the spec elements.

There was an issue on github https://github.com/onsi/ginkgo/issues/462 which suits my use case, but the OP misunderstood and it took a different turn.

答案1

得分: 1

是的,这是可能的。我之前尝试过类似的操作而没有遇到任何问题。每次循环运行时都应该是一个新的测试用例。因此,当一个测试实际失败时,给它一个新的名称以与其他测试区分开来。

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // 每次运行套件时可以有所变化
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
          When("输入为"+s, func(){
             It("匹配输出", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
          })
        }
    })
})

你在当前的代码中遇到了什么问题吗?

英文:

Yes, It is possible. I have tried similar before without any issue. Every time the loop actually run should be a new test case. So, give it a new name to distinguish it from other when one test actually fails.

var _ = Describe("Outer", func() {
    var stringArray []string

    BeforeSuite(func() {
        stringArray = []string{"a", "b", "c", "d", "e"} // can vary every time i run the suite
    })


    Describe("test 1", func() {
        for _, s := range stringArray {
          When("Input is "+s, func(){
             It("match the output", func() {
                print(s)
                Expect(s).ToNot(Equal("f"))
            })
          })
        }
    })
})

Did you face any issue in your current code?

huangapple
  • 本文由 发表于 2021年8月5日 19:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68665342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定