有没有办法在单元测试期间忽略来自第三方包的测试错误?

Is there a way to ignore test errors coming from third party packages during unit tests?

由于与测试本身无关的第 3 方错误,我的测试失败了。基本上我必须使用的一些测试服务器无法在 Windows OS 上关闭,但实际上运行良好。

我需要忽略它产生的错误,但它们在 defer 部分,如下所示。有没有办法完全忽略来自前两行的任何错误?

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")
    }
}

测试因此在他们的代码中死亡

Is there a way to ignore test errors coming from third party packages during unit tests?

不,不是你的情况,因为测试不是“来自第三方包”:测试是你的,但失败了。您的代码从“第三方包”调用函数这一事实在这里没有任何意义。

将它移至另一个命名空间没有帮助,上述有用的评论也没有帮助。

我随身携带并用于创建新的 Nomad 测试服务器的 testing.T 结构允许第 3 方代码无法通过我的测试。不使用它并使用如下所示的新对象可以解决此问题。

nomadTestUtil.NewTestServer(&testing.T{},...