如何与消费者分享Golang包测试数据文件?

我们创建了一个私有 go 库(实用工具包),用于在服务之间共享通用 API 方法和业务逻辑。在实用程序包中,我们在“.json”文件中有几十个模拟JSON响应,用于模拟API测试。


使用此实用程序包的服务也希望访问相同的模拟文件,因为它们依赖于相同的模拟 API 响应来测试各种内部业务逻辑。有没有办法通过一些相对文件路径或预编译它们(字节或字符串)来共享这些文件,以允许使用者测试在通过标准pkg变量或方法导入后引用相同的.json文件(文件路径或数据)?go get github.com/whatever/utility-library


理想情况下,使用者测试可以通过子包(如“http/httptest”)访问这些文件,然后在自己的模拟服务器(如 or 等)中引用内部 .json 文件。我们希望继续将模拟响应存储在同一实用程序模块内的 .json 文件中,但只需将它们公开给使用者测试文件,严格用于测试目的。httptest.GetBusinessObject.Response []byteResponseFilePath string


my-api-pkg

├── go.mod

└── api

    └── api.go

    └── api_test.go // <= we currently access .json files here like utiltest.apiResponse []byte

    └── apitest // <= sub pkg that contains testing methods and .json accessors

        └── apitest.go

        └── responses

            └── api.response.json

my-service-pkg

├── go.mod

├── server.go

├── server_test.go

└── sub-pkg

    └── subpkg.go

    └── subpkg_test.go // <= want to access utiltest.apiResponse []byte for api mocks here



郎朗坤
浏览 84回答 2
2回答

明月笑刀无情

非 Go 文件和文件不会编译到模块中。若要发布文件,请将其重命名为文件,并导出要向客户端公开的变量和函数。_test.go_test.go.go对于非 Go 文件,从 Go 1.16 开始,请嵌入它们:package mycompany.com/common/testingimport _ "embed"//go:embed responses/api.response.jsonvar MockApiJsonResponse []byte // or string其中,目录树如下所示:testing&nbsp;└── testing.go└── responses&nbsp; &nbsp; └── api.response.json然后,您的客户端将能够像往常一样引用这些导出的变量和函数:package mycompany.com/serviceimport (&nbsp; &nbsp; "testing"&nbsp; &nbsp; common_testing "mycompany.com/common/testing")func TestThings(t *testing.T) {&nbsp; &nbsp; mock := common_testing.MockApiJsonResponse&nbsp; &nbsp; // ...}

红颜莎娜

OP 答案!看来我的问题是我的软件包中有一个导入的实用程序fn,来自它自己的内部testing.go文件,这意味着它的init()函数正在运行并污染上游的pkg测试运行。apitest我最初的方法很合理,但是在我删除了内部测试后,erptest pkg不再下载到上游服务。我更改了结构以引用根目录,如下所示,这恢复了 apitest pkg 的 upsrtream 下载:/my-api-pkg├── go.mod└── /api&nbsp; &nbsp; └── api.go&nbsp; &nbsp; └── api_test.go // <= we currently access .json files here like utiltest.apiResponse []byte&nbsp; &nbsp; └── /apitest // <= sub pkg that contains testing methods and .json accessors&nbsp; &nbsp; &nbsp; &nbsp; └── apitest.go└── /testing // <= moving the files here re-enabled download and access or erptest code to upstream consumers&nbsp; &nbsp; └── /files&nbsp; &nbsp; &nbsp; &nbsp; └── /api.response.json这是我的apitest pkg导出的基本结构,以便通过以下方式访问上游的文件(作为[]字节)apitest.Domain().GetApiRoute1.Res1// accessortype domainAccessor struct {&nbsp; &nbsp; GetApiRoute1 getApiRoute1&nbsp; &nbsp; ...}func DomainAccessor() domainAccessor {&nbsp; &nbsp; return domainAccessor{&nbsp; &nbsp; &nbsp; &nbsp; GetApiRoute1: _GetApiRoute1,&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}// individual file accessors for each routetype getApiRoute1 struct {&nbsp; &nbsp; Res1 []byte&nbsp; &nbsp; Res2 []byte}var _GetApiRoute1 = getApiRoute1{&nbsp; &nbsp; Res1: loadFile("testing/files/api/v1/domain/getApiRoute1.res.json"),&nbsp; &nbsp; Res2: loadFile("testing/files/api/v1/domain/getApiRoute2.res.json"),}加载文件 fn 将文件读取到 []bytefunc loadFile(filepath string) []byte {&nbsp; &nbsp; dir := ""&nbsp; &nbsp; _, filename, _, ok := runtime.Caller(0)&nbsp; &nbsp; if ok {&nbsp; &nbsp; &nbsp; &nbsp; dir = path.Join(path.Dir(filename), "..", "..", "..")&nbsp; &nbsp; }&nbsp; &nbsp; fullPath := path.Join(dir, filepath)&nbsp; &nbsp; body, err := ioutil.ReadFile(fullPath)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Println("Error apitest.loadFile: unable to read file", err)&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; return body}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go