t.Helper undefined (type *testing.T has no field or method Helper)

Following along with “learn-go-with-tests”.

Issue: Attempting to use the source code here in Visual Studio Code:

package main

import “testing”

func TestHello(t *testing.T) {

	assertCorrectMessage := func(t *testing.T, got, want string) {
	t.Helper()
		if got != want {
			t.Errorf("got %q want %q", got, want)
		}
	}

	t.Run("saying hello to people", func(t *testing.T) {
		got := Hello("Chris")
		want := "Hello, Chris"
		assertCorrectMessage(t, got, want)
	})

	t.Run("empty string defaults to 'world'", func(t *testing.T) {
		got := Hello("")
		want := "Hello, World"
		assertCorrectMessage(t, got, want)
	})

}

Receiving following errors:

t.Helper undefined (type *testing.T has no field or method Helper)
t.Run undefined (type *testing.T has no field or method Run)
t.Run undefined (type *testing.T has no field or method Run)

I am thinking it may have to do with my setup of Go in VSCode. I succesfully ran a short test in the beginning modules but not running into errors when accessing ‘t’ methods.

Any help is greatly appreciated, thank you!

2 Likes

I ran your code in the local Linux system. It’s working fine. Yes, it should be related to VS Code settings. Is the VS configured properly to access GOROOT’s contents?

In case you need, I append a VS guide here (Go with Visual Studio Code). That’s as far as I can go due to not having any Windows machine around me. :zipper_mouth_face:

package main

import (
	"testing"
)

func Hello(name string) string {
	if name != "" {
		return "Hello, 1" + name
	}
	return "Hello, World"
}

func TestHello(t *testing.T) {
	assertCorrectMessage := func(t *testing.T, got, want string) {
		t.Helper()
		if got != want {
			t.Errorf("got %q want %q", got, want)
		}
	}

	t.Run("saying hello to people", func(t *testing.T) {
		got := Hello("Chris")
		want := "Hello, Chris"
		assertCorrectMessage(t, got, want)
	})

	t.Run("empty string defaults to 'world'", func(t *testing.T) {
		got := Hello("")
		want := "Hello, World"
		assertCorrectMessage(t, got, want)
	})

}
\\ Test Output:
\\ --- FAIL: TestHello (0.00s)
\\    --- FAIL: TestHello/saying_hello_to_people (0.00s)
\\        main_test.go:25: got "Hello, 1 Chris" want "Hello, Chris"
\\ FAIL
\\ FAIL    gosandbox       0.001s
\\ FAIL
2 Likes

@hollowaykeanho Thank you so much for the feedback!

I looked into my /src/testing.go and realized I was missing the Helper function and the Run function.

hmmm…then I realized the instructions I followed to download Go on Linux was for an older version. I have uninstalled that version and now am running 1.13.3. with the correct functions needed.

:ok_man:

2 Likes

You’re welcome. Would you mind mark the appropriate post as “marked as solved” please? This is to indicate others that the case is solved and they can work on other questions. :grin:

Feel free to ask a different question again.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.