使用gocheck来测试Go(golang)代码时,suite函数是如何工作的?

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

Using gocheck for testing go (golang) code, how does the suite function work?

问题

我正在使用gocheck来测试我的Go代码。我参考了以下示例(类似于他们网站提供的示例):

package hello_test

import (
	"testing"

	gocheck "gopkg.in/check.v1"
)

// 将gocheck集成到"go test"运行器中。
func Test(t *testing.T) {
	gocheck.TestingT(t)
}

type MySuite struct{} // <==== 这个结构体必须这样命名吗?如果我们有多个这样的结构体并注册它们,会有问题吗?

var _ = gocheck.Suite(&MySuite{}) // <==== 这行代码是做什么的?

func (s *MySuite) TestHelloWorld(c *gocheck.C) {
	c.Assert(42, gocheck.Equals, "42")
	c.Check(42, gocheck.Equals, 42)
}

然而,有一些代码行我在阅读文档后仍然不太理解。为什么需要type MySuite struct{}这一行,更有趣的是,为什么需要var _ = gocheck.Suite(&MySuite{})这一行?第一行很容易推断出来,可能需要先声明该结构体,并创建按照示例中所示签名实现测试的函数。然而,第二行让我困惑不已。文档中说:

Suite将给定的值注册为要运行的测试套件。给定值中以Test前缀开头的任何方法都将被视为测试方法。

然而,我对很多事情都不确定。例如,如果我在同一个文件中使用多个MySuite结构体运行此函数会有问题吗?MySuite struct类型有什么特殊之处吗?即使注册了一些不同的结构体,gocheck测试套件是否仍然可以工作?基本上,我们可以在一个文件中注册多少次结构体,它仍然可以工作吗?

英文:

I was trying to use gocheck to test my go code. I was guiding myself with the following example (similar to the one provided by their website):

package hello_test

import (
	&quot;testing&quot;

	gocheck &quot;gopkg.in/check.v1&quot;
)

// Hook up gocheck into the &quot;go test&quot; runner.
func Test(t *testing.T) {
	gocheck.TestingT(t)
}

type MySuite struct{} //&lt;==== Does the struct have to be named that way, what if we have multiple of these and register them, is it a problem?

var _ = gocheck.Suite(&amp;MySuite{}) // &lt;==== What does this line do?

func (s *MySuite) TestHelloWorld(c *gocheck.C) {
	c.Assert(42, gocheck.Equals, &quot;42&quot;)
	c.Check(42, gocheck.Equals, 42)
}

However, there are some lines I am not sure I understand even after reading the documentation. Why is the line type MySuite struct{} needed and even more of an interesting line, why is var _ = gocheck.Suite(&amp;MySuite{}) needed? The first one, its easy to infer that one probably has to declare that struct first and create functions that will run the tests if implemented with the signature as shown. However, the second line beats me. I have literally no idea why its needed. The documentation says:

> Suite registers the given value as a test suite to be run. Any methods starting with the Test prefix in the given value will be considered as a test method.

However, I am not sure about a lot of things. For instance, is there a problem if I run this function with multiple MySuite structs in the same file? Is there anything special about the type MySuite struct? Could the gocheck testing suite work even with some different struct being registered? Basically, how many times can we register a struct in one file and will it still work?

答案1

得分: 4

gocheck.Suite函数具有将给定的测试套件值注册到gocheck包的副作用。在内部,它只是将测试套件添加到已注册测试套件的切片中。你也可以通过以下方式实现相同的效果:

func init() {
    gocheck.Suite(&MySuite{})
}

两种形式都可以工作,所以只是风格上的区别。

当你调用gocheck.TestingT时,注册的测试套件中的测试将会运行。你可以在名为Test的测试函数中调用它,这样Go的测试框架就会自动识别它。这就是将gocheck测试集成到测试框架中的方式。请注意,你只需要调用一次TestingT来运行所有的测试套件,而不是为每个测试套件都调用一次。

英文:

The gocheck.Suite function has the side effect of registering the given suite value with the gocheck package. Internally, it just adds the the suite to a slice of registered test suites. You could get the same effect with:

func init() {
    gocheck.Suite(&amp;MySuite{})
}

Either form should work, so it is just a matter of style.

The tests in the registered test suites are run when you call gocheck.TestingT. You do this in your test called Test, which will be picked up by Go's testing framework. This is how gocheck tests are integrated into the testing framework. Note that you only need a single invocation of TestingT to run all test suites: not one for each test suite.

huangapple
  • 本文由 发表于 2014年6月25日 10:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/24399104.html
匿名

发表评论

匿名网友

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

确定