As you know _test.go are ignored when go projects is build and mock package is only imported by _test.go files so if these files are not include in builded project why to simply include the mock package. So was wondering how to ignore the files inside it when building project.
Tried adding suffix _test.go to the files in mock package but got but an error "MockStruct not declared by package mock" when used.
Also tried to use build constraints
//go:build ignore
Got same error "MockStruct not declared by package mock"
Am i missing something here? Is using build constraints the only way?
CodePudding user response:
If your mock is being used only on test files it is not imported when building the project. Go compiler does not include tests and its dependencies when building.
Try this as an example:
- Build the following code;
- Check its binary size;
- Remove the
sample_test.go
file; - Build again and check its binary size;
Size before and after tests should not be different, and it proves that nothing from test is included in the build.
sample.go
package main
import "fmt"
type SampleInterface interface {
DoSomething()
}
type Sample struct {
Name string
}
func main() {
s := Sample{}
CallDoSomething(&s)
}
func (s *Sample) DoSomething() {
fmt.Println("Do Something implementation ", s.Name)
}
func CallDoSomething(si SampleInterface) {
si.DoSomething()
}
sample_test.go
package main
import (
"fmt"
"testing"
)
type sample_mock struct {
Name string
}
func (s *sample_mock) DoSomething() {
fmt.Println("Do Something implementation", s.Name)
}
func TestCallDoSomething(t *testing.T) {
s := sample_mock{
Name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
}
CallDoSomething(&s)
}