使用Go测试资源文件

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

Test resource files with Go

问题

我是一名Java开发者,大部分项目都使用Maven。项目结构非常简单:

  - src
    - main
      - java
      - resources
    - test
      - java
      - resources

通常情况下,我会将测试所需的文件(如JSON、SQL或其他与测试相关的文件)放在test/resources文件夹中。

现在,我在使用Go进行开发时,发现测试代码与源代码放在同一个包(文件夹)中。但是,我不知道应该把与测试相关的资源放在哪里。有人有什么建议吗?

我只是不知道应该把与测试相关的资源放在哪里。我可以把文件放在同一个包中,但对我来说,这样的分离并不好。

英文:

I'm a java developer and I'm using Maven for most of my projects. The structure is quite simple:

  - src
    - main
      - java
      - resources
    - test
      - java
      - resources

Normally, for tests, I'm using the test/resources folder for files like JSON, SQL or anything else related to the tests.

Now, developing in Go, I see that tests are in the same package (folder) as the code. But where do you put the resources related to tests? Does anyone have tips on this?

Just that I don't really know where to put my resources related to the tests. I can put my files in the same package, but for me it's not a good segregation.

答案1

得分: 2

实际上,有一个特殊的文件夹名称用于此目的:testdata
似乎这并没有很好地记录在文档中,正如在这个 GitHub 问题中所述。

Go 编译器会忽略名为 testdata 的文件夹。这不仅是一种命名约定,还在工具链中得到了特殊处理。

正如文档中所述

> go 工具将忽略名为 "testdata" 的目录,使其可用于保存测试所需的辅助数据。

英文:

Actually there is a special folder name for that: testdata.
It seems this is not well documented, as stated in this GitHub issue.

The Go compiler ignores folders named testdata. It is not only convention to name your folder that, it is also handled specifically in the toolchain.

As said in the docs:

> The go tool will ignore a directory named "testdata", making it available to hold ancillary data needed by the tests.

huangapple
  • 本文由 发表于 2023年7月13日 02:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76673640.html
匿名

发表评论

匿名网友

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

确定