错误 vs 致命错误在测试中的区别

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

Error vs Fatal in tests

问题

我正在使用Go-Json-Rest开发一个JSON Web服务,并编写测试代码。

...
recorded = test.RunRequest(t, &api.Handler,
	test.MakeSimpleRequest("POST", "http://localhost/api/products",
		product))
recorded.CodeIs(201)
recorded.ContentTypeIsJson()

var newProduct Product
err := recorded.DecodeJsonPayload(&newProduct)
if err != nil {
	t.Fatal(err)
}
...

在Python中,我习惯使用Fatal,因为它会立即停止测试用例的执行。这是有道理的:如果数据不是JSON,为什么要尝试解码呢?

但是我看到recorded.CodeIs(201)recorded.ContentTypeIsJson()和其他测试使用了Error,它不会停止测试的执行。

在测试中,我应该使用Error还是Fatal

英文:

I am developing a JSON web service using Go-Json-Rest. I am writing tests.

...
recorded = test.RunRequest(t, &api.Handler,
	test.MakeSimpleRequest("POST", "http://localhost/api/products",
		product))
recorded.CodeIs(201)
recorded.ContentTypeIsJson()

var newProduct Product
err := recorded.DecodeJsonPayload(&newProduct)
if err != nil {
	t.Fatal(err)
}
...

I am using Fatal as I am coming from Python world where an assert would immediately stop test case method execution. And this make sense: why trying to decode the data, if it's not JSON?

But recorded.CodeIs(201), recorded.ContentTypeIsJson() and other tests I've seen use Error which doesn't stop test execution.

What should I use in tests? Error or Fatal?

答案1

得分: 31

我认为在继续运行测试不可能提供任何有用的调试信息之前,你可以使用Error,然后使用Fatal。如果你不确定(比如在编写一个被用于许多不同测试上下文中的方法,比如CodeIs),那么就选择Error,因为继续运行测试通常不会造成伤害。

按照这个标准,当JSON解码失败时使用Fatal是有意义的,因为在此之后,如你所说,不会发生任何有趣的事情。而且可以理解为什么CodeIsContentTypeIsJson使用Error,因为它们是将在不同测试中使用的方法。

一个不同的例子可能更好地说明为什么在你知道不会发生其他有趣事情之前使用Error:假设你想对JSON响应进行多个不同方面的合理性检查,其中任何一个子集都可能出错(比如,你的产品API可能使用错误的类型返回price,或者在不需要时未返回空的description等)。对于每个检查,使用Error而不是Fatal意味着你的测试将始终运行所有检查,并报告哪些检查失败。

英文:

I think you use Error until continuing to run the test can't possibly give you any more information useful in debugging, then you use Fatal. And if you're not sure (like if you're writing a factored-out method like CodeIs to be used in the context of lots of different tests), go for Error, since you're generally not doing harm by continuing to run the test.

By that criteria, it makes sense for you to Fatal at failed JSON decoding after which, as you say, nothing interesting is going to happen. And it's understandable that CodeIs and ContentTypeIsJson use Error because they're methods that are going to be used across different tests.

A different example might better illustrate why to use Error until you know nothing else interesting will happen: say you want to sanity-check several different things about the JSON response, and any subset them could be wrong. (Like, your product API could return price using the wrong type, or it could fail to return empty descriptions when you don't want that, or...) Using Error instead of Fatal for each check means your test will always run them all and report which ones failed.

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

发表评论

匿名网友

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

确定