Call a function from within another test file

Hi,

I have two test files and I am calling a function from within another test file. However, I get undefined errors while running the tests. Am I missing something here?

Thanks

This on its own works fine

setUp
hello
test
tearDown
bye
# internal/lib/client_test.go

package client

import (
	"fmt"
	"testing"
)

var a string

func setUp() {
	a = "hello"

	fmt.Println("setUp")
}

func tearDown() {
	a = "bye"

	fmt.Println("tearDown")
}

func TestController_Create(t *testing.T) {
	setUp()
	fmt.Println(a)
	fmt.Println("test")
	tearDown()
	fmt.Println(a)
}

This doesn’t work

undefined: setUp
undefined: a
undefined: tearDown
undefined: a
# internal/lib/client_test.go

package client

import (
	"fmt"
	"testing"
)

func TestController_Create(t *testing.T) {
	setUp()
	fmt.Println(a)
	fmt.Println("test")
	tearDown()
	fmt.Println(a)
}
# internal/lib/common_test.go

package client

import (
	"fmt"
)

var a string

func setUp() {
	a = "hello"
	fmt.Println("setUp")
}

func tearDown() {
	a = "bye"
	fmt.Println("tearDown")
}

Looks like it doesn’t work only when it is called like below otherwise it does work.

go test ./internal/lib/client_test.go ./internal/lib/client.go

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