在子目录Golang测试 [英] Golang tests in sub-directory

查看:837
本文介绍了在子目录Golang测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个围棋封装测试和示例包作为子目录,以保持工作区清洁。这是可能的,如果是怎样?

I want to create a package in Go with tests and examples for the package as subdirectories to keep the workspace cleaner. Is this possible and if so how?

所有的文档始终把测试code在同一个地方与其他code,以某种方式或只是约定这更好的?

All the documentation always puts the testing code in the same place as the other code, is this better in some way or just convention?

感谢。

推荐答案

请注意,您的可以的运行去测试递归:你需要为列表中的所有包你想测试

Note that you can run go test "recursively": you need to list all the packages you want to test.

如果你是你的围棋项目的根文件夹中,键入:

If you are are the root folder of your Go project, type:

go test ./...

/ ... 符号在节中描述的装箱单说明的中的命令

The './...' notation is described in the section "Description of package lists" of the "command go":

这是导入路径是一个模式,如果它包含一个或多个 ... 通配符,每一个可以匹配任何字符串,包括空字符串和字符串包含斜杠。

An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes.

这样的模式扩展到在 GOPATH 树木找到相匹配的模式名的所有软件包目录。

Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.

作为一个特例, X /...匹配 X 以及 X 的子目录。结果
  例如,净/...扩展到及其子目录中的包。

As a special case, x/... matches x as well as x's subdirectories.
For example, net/... expands to net and packages in its subdirectories.

如果你把你的 _test.go 文件中的子文件夹中,去测试./... 命令就可以接他们回家。结果
但是:


If you keep your _test.go files in a subfolder, the 'go test ./...' command will be able to pick them up.
But:


  • 您将需要preFIX导出的变量和函数与您包的名称(在你的测试中使用),为了测试文件,以便能够访问包导出的内容。

  • 您不能访问非导出的内容。

话虽这么说,我还是preFER保持 _test.go 文件右侧的主源文件的旁边。它是更容易找到

That being said, I would still prefer keep the _test.go file right beside the main source file: it is easier to find.

这篇关于在子目录Golang测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆