英文:
Is there a way to ignore test errors coming from third party packages during unit tests?
问题
我的测试由于一个与测试本身无关的第三方错误而失败。基本上,我必须使用的某个测试服务器在Windows操作系统上无法正常关闭,但实际上运行良好。
我需要忽略它生成的错误,但它们在下面的延迟部分中。有没有办法完全忽略来自前两行的任何错误?
func TestDoSomething(t *testing.T) {
testServer := setupTestNomad(t) // 包含第三方测试服务器的创建
defer testServer.Stop() // 实际上在这里由于第三方代码本身的问题而失败
data := DoSomething()
if data == nil { // 数据不为空,一切正常
t.Errorf("失败,数据为空")
}
}
测试由于他们的代码中的这个问题而失败:
英文:
My tests are failing due to a 3rd party error that is irrelevant to the tests themselves. Basically some testserver I have to use fails to shutdown on Windows OS but actually runs fine.
I need to ignore the errors it generates, but they are in the defer part as below. Is there a way to completely ignore any errors coming from the first two lines?
func TestDoSomething(t *testing.T) {
testServer := setupTestNomad(t) //contains 3rd party test server creation
defer testServer.Stop() //actually fails here in the 3rd party struct due to 3rd party code itself
data := DoSomething()
if data == nil { //data is not null and all is fine here
t.Errorf("Failed, data null")
}
}
Test dies because of this within their code
答案1
得分: 1
在单元测试期间,有没有办法忽略来自第三方包的测试错误?
不,在你的情况下不行,因为测试不是来自第三方包:测试是你自己的,而且它失败了。你的代码调用了一个来自“第三方包”的函数在这里没有意义。
英文:
> Is there a way to ignore test errors coming from third party packages during unit tests?
No, not in your case because the test is not "from third party package": The test is yours and it fails. The fact that your code calls a function from a "third party package" has no meaning here.
答案2
得分: 0
将其移动到另一个命名空间并没有帮助,上面的有用评论也没有帮助。
我携带并使用testing.T
结构体来创建一个新的Nomad测试服务器,这使得第三方代码可以使我的测试失败。不使用它,而是使用下面的新对象可以解决这个问题。
英文:
Moving it to another namespace didn't help nor did the above helpful comments.
The testing.T
struct I carry around and use to create a new Nomad Test server allows the 3rd party code to fail my test. Not using that and using a new object as below solves this problem.
nomadTestUtil.NewTestServer(&testing.T{},...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论