按照预定义的顺序执行测试用例。

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

Execute test cases in a pre-defined order

问题

有没有一种方法可以按照预定义的顺序在GoLang中执行测试用例?

附言:我正在为事件的生命周期编写测试用例。因此,我对所有CURD操作都有不同的API。我希望按照特定的顺序运行这些测试用例,因为只有在创建事件后才能销毁它。

另外,我能否从一个测试用例中获取一些值,并将其作为输入传递给另一个测试用例。(例如:为了测试删除事件的API,我需要一个事件ID,这个ID可以在调用create_event测试用例时获取)

我对GoLang还不熟悉,有人可以指导我吗?

提前感谢。

英文:

Is there a way to execute test cases in GoLang in a pre-defined order.

P.S: I am writing test cases for life cycle of a event. So I have different api's for all the CURD operations. I want to run these test cases in a particular order as only if an event is created it can be destroyed.

Also can I get some value from one test case and pass it as input to another. (example:- To test the delete event api, I need a event_id which i get when I call create_event test case)

I am new to GoLang, can someone please guide me through.

Thanks in advance

答案1

得分: 7

唯一的方法是将所有的测试封装到一个测试函数中,该函数按正确的顺序和上下文调用子函数,并将testing.T指针传递给每个函数,以便它们可以失败。缺点是它们将被视为一个测试。但实际上,测试框架来说,测试是无状态的,每个函数都是一个单独的测试用例。

请注意,尽管测试可能按照编写的顺序运行,但我没有找到任何说明这实际上是某种契约的文档。因此,即使您可以按顺序编写它们并将状态保持为外部全局变量 - 这是不推荐的。

自从Go 1.4以来,框架给您的唯一灵活性是TestMain方法,它允许您运行前/后步骤或设置/拆卸:

func TestMain(m *testing.M) {
    if err := setUp(); err != nil {
        panic(err)
    }
    rc := m.Run()
    tearDown()
    os.Exit(rc)
}

但这不会给您想要的结果。安全地做到这一点的唯一方法是这样做:

// 这是整个有状态测试序列 - 对于测试框架来说,它只是一个测试用例
func TestWrapper(t *testing.T) {
    // 假设您将上下文作为某个包含结构体传递
    ctx := new(context)
    test1(t, ctx)
    test2(t, ctx)
    ...
}

// 这个结构体在方法之间保存上下文
type context struct {
    eventId string
}

func test1(t *testing.T, c *context) {
    // 做你的事情,并且你可以操作上下文
    c.eventId = "something"
}

func test2(t *testing.T, c *context) {
    // 做你的事情,并且你可以操作上下文
    doSomethingWith(c.eventId)
}
英文:

The only way to do it is to encapsulate all your tests into one test function, that calls sub-functions in the right order and with the right context, and pass the testing.T pointer to each so they can fail. The down-side is that they will all appear as one test. But in fact that is the case - tests are stateless as far as the testing framework is concerned, and each function is a separate test case.

Note that although the tests may run in the order they are written in, I found no documentation stating that this is actually a contract of some sort. So even though you can write them in order and keep the state as external global variables - that's not recommended.

The only flexibility the framework gives you since go 1.4 is the TestMain method that lets you run before/after steps, or setup/teardown:

func TestMain(m *testing.M) {

	if err := setUp(); err != nil {
		panic(err)
	}
	rc := m.Run()
	tearDown()
	os.Exit(rc)
}

But that won't give you what you want. The only way to do that safely is to do something like:

// this is the whole stateful sequence of tests - to the testing framework it's just one case
func TestWrapper(t *testing.T) {

   // let's say you pass context as some containing struct
   ctx := new(context)
   test1(t, ctx)
   test2(t, ctx)
   ...
}

// this holds context between methods
type context struct {   
    eventId string
}

func test1(t *testing.T, c *context) {
   // do your thing, and you can manipulate the context
   c.eventId = "something"
} 

func test2(t *testing.T, c *context) {
   // do your thing, and you can manipulate the context
   doSomethingWith(c.eventId)
} 

huangapple
  • 本文由 发表于 2015年5月11日 23:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/30171693.html
匿名

发表评论

匿名网友

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

确定