Suppose I have a type MyType
with a private method (mt *MyType) private()
in a package mypackage
. I also have a directory tests
, where I want to store tests for my package. This is how tests/mypackage_test.go
looks like:
package mypackage_test
import (
"testing"
"myproj/mypackage"
)
func TestPrivate(t *testing.T) {
// Some test code
}
However, when I run go test
I get the cannot refer to unexported field or method my package.(*MyType)."".private)
error. I've googled a bit and found out that functions starting with lower case can not be seen outside their own package (and this seems to be true, 'cause upper case functions are freely callable from the tests).
I also read somewhere that adding <...>_internal_test.go
to the test file could solve my problem like this (tests/mypackage_internal_test.go
):
package mypackage
import (
"testing"
)
func TestPrivate(t *testing.T) {
mt := &MyType{}
// Some test code
}
But with this I only get undefined: MyType
. So, my question: how can I test internal/private methods?
Aucun commentaire:
Enregistrer un commentaire