担架/证明/断言只是给出堆栈跟踪,缺少实际错误消息

时间:2018-10-23 14:45:52

标签: go

我有一个以下测试,我想转换为使用github.com/stretchr/testify/assert导入,完成此操作的最佳实践是什么? 现在的代码:

func TestSdk(t *testing.T) {
    ctx := context.Background()

    sdk, err := NewSdk(ctx)
    if err != nil {
        t.Errorf("Unable to get VMware SDK: %v", err)
    }

    defer sdk.GovClient.Logout(ctx)
}
  

错误:失败| ---失败:TestSdk(0.00秒)        | sdk_test.go:48:无法获取VMware SDK:请设置环境变量:HCI_ENDPOINT,HCI_USERNAME和HCI_PASSWORD        |紧急:运行时错误:无效的内存地址或nil指针取消引用[已恢复]        |紧急:运行时错误:无效的内存地址或nil指针取消引用        | [信号SIGSEGV:细分违规代码= 0x1 addr = 0x0 pc = 0x151cc31]        | goroutine 6 [运行中]:        | testing.tRunner.func1(0xc00019a100)        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:792 + 0x387        |紧急(0x1642c20,0x1f46a80)        | /usr/local/Cellar/go/1.11.1/libexec/src/runtime/panic.go:513 + 0x1b9        | github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo.TestSdk(0xc00019a100)        | /Users/jonma/go/src/github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo/sdk_test.go:51   + 0x121        | testing.tRunner(0xc00019a100,0x1825b48)        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:827 + 0xbf        |通过测试创建。(* T)。运行        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:878 + 0x353失败| github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo 0.019s

这就是我的更改,这种方法的问题在于,错误消息不会仅仅显示一些长堆栈跟踪

func TestSdk(t *testing.T) {
    ctx := context.Background()

    sdk, err := NewSdk(ctx)

    assert.Errorf("Unable to get VMware SDK: %v", err)

    defer sdk.GovClient.Logout(ctx)
}
  

失败| ---失败:TestSdk(0.00秒)        |紧急:运行时错误:无效的内存地址或nil指针取消引用[已恢复]        |紧急:运行时错误:无效的内存地址或nil指针取消引用        | [信号SIGSEGV:细分违规代码= 0x1 addr = 0x0 pc = 0x151eecf]        | goroutine 6 [运行中]:        | testing.tRunner.func1(0xc00019e100)        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:792 + 0x387        |紧急(0x1645200,0x1f4aab0)        | /usr/local/Cellar/go/1.11.1/libexec/src/runtime/panic.go:513 + 0x1b9        | github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo.TestSdk(0xc00019e100)        | /Users/jonma/go/src/github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo/sdk_test.go:57   + 0x1ef        | testing.tRunner(0xc00019e100,0x1828420)        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:827 + 0xbf        |通过测试创建。(* T)。运行        | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:878 + 0x353失败| github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo 0.034s

在这里,我缺少所调用函数的实际错误消息。

1 个答案:

答案 0 :(得分:0)

尝试使用composer代替defer sdk.GovClient.Logout(ctx)

第二次恐慌是由于不正确的ctx造成的。

(我这里没有vmwareSDK,关键是在尝试之前sdk.GovClient.Logout是否具有有效参数)