Reputation: 814
I have this following test that I would like to convert to use github.com/stretchr/testify/assert
import, what would be best practice to get this done ?
Code as is now:
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)
}
Error: FAIL | --- FAIL: TestSdk (0.00s) | sdk_test.go:48: Unable to get VMware SDK: Please set environment variables: HCI_ENDPOINT, HCI_USERNAME and HCI_PASSWORD | panic: runtime error: invalid memory address or nil pointer dereference [recovered] | panic: runtime error: invalid memory address or nil pointer dereference | [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x151cc31] | goroutine 6 [running]: | testing.tRunner.func1(0xc00019a100) | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:792 +0x387 | panic(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 | created by testing.(*T).Run | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:878 +0x353 FAIL | github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo 0.019s
This is what I changed to, the problem with this approach is that, error message gets not displayed just some long stack trace
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)
}
FAIL | --- FAIL: TestSdk (0.00s) | panic: runtime error: invalid memory address or nil pointer dereference [recovered] | panic: runtime error: invalid memory address or nil pointer dereference | [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x151eecf] | goroutine 6 [running]: | testing.tRunner.func1(0xc00019e100) | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:792 +0x387 | panic(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 | created by testing.(*T).Run | /usr/local/Cellar/go/1.11.1/libexec/src/testing/testing.go:878 +0x353 FAIL | github.com/kubicorn/kubicorn/cloud/vmware/vmwareSdkGo 0.034s
Here I'm missing the actual error message from the function that was called.
Upvotes: 3
Views: 1581
Reputation: 1168
The error message you quoted is not from the code snippet you posted. But I do see three obvious issues:
t
, I think when you saw the panic you probably used nil
.assert.NoErrorf
, this will fail when there is an error.Putting this all together:
func TestSdk(t *testing.T) {
ctx := context.Background()
sdk, err := NewSdk(ctx)
assert.NoErrorf(t, err, "Unable to get VMware SDK: %v", err)
defer sdk.GovClient.Logout(ctx)
}
Upvotes: 0
Reputation: 34327
Instead of defer sdk.GovClient.Logout(ctx)
try defer func() { if (ctx!=nil) { sdk.GovClient.Logout(ctx) }
The second panic is from this defer on an incorrect ctx
(I don't have the vmwareSDK in front of me here, the key thing is to check that the sdk.GovClient.Logout has a valid parameter before attempting it)
Upvotes: 0