如何在构建Go项目时忽略模拟包?

huangapple go评论94阅读模式
英文:

How to ignore mock package when building go project?

问题

如你所知,当构建 Go 项目时,_test.go 文件会被忽略,而 mock 包只会被 _test.go 文件导入。所以,如果这些文件不包含在构建的项目中,为什么要简单地包含 mock 包呢?因此,我想知道在构建项目时如何忽略其中的文件。

尝试将 mock 包中的文件添加后缀 _test.go,但在使用时出现了错误 "MockStruct not declared by package mock"。

还尝试使用构建约束:

//go:build ignore

但是得到了相同的错误 "MockStruct not declared by package mock"。

我在这里漏掉了什么吗?使用构建约束是唯一的方法吗?

英文:

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?

答案1

得分: 3

如果您的模拟仅在测试文件中使用,那么在构建项目时不会导入它。Go编译器在构建时不会包含测试及其依赖项。

尝试以下示例:

  • 构建以下代码;
  • 检查其二进制文件大小;
  • 删除sample_test.go文件;
  • 再次构建并检查其二进制文件大小;

测试前后的大小不应有差异,这证明测试中的内容未包含在构建中。

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)
}
英文:

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)
}

huangapple
  • 本文由 发表于 2022年3月29日 12:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/71656403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定