简体   繁体   English

子目录中的 Golang 测试

[英]Golang tests in sub-directory

I want to create a package in Go with tests and examples for the package as subdirectories to keep the workspace cleaner.我想在 Go 中创建一个包,其中包含该包的测试和示例作为子目录,以保持工作空间更清洁。 Is this possible and if so how?这可能吗?如果可以,怎么办?

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 .请注意,您可以“递归”运行go test :您需要列出所有要测试的包

If you are in the root folder of your Go project, type:如果您位于 Go 项目的根文件夹中,请键入:

go test ./...

The ' ./... ' notation is described in the section " Description of package lists " of the " command go ": ' ./... ' 符号在“命令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.如果导入路径包含一个或多个“ ... ”通配符,则它是一种模式,每个通配符都可以匹配任何字符串,包括空字符串和包含斜杠的字符串。

Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.这样的模式扩展到所有在GOPATH树中找到的名称与模式匹配的包目录。

As a special case, x/... matches x as well as x 's subdirectories.作为一种特殊情况, x/...匹配x以及x的子目录。
For example, net/... expands to net and packages in its subdirectories.例如, net/...在其子目录中扩展为net和 packages。


If you keep your _test.go files in a subfolder, the ' go test ./... ' command will be able to pick them up.如果您将_test.go文件保存在子文件夹中,则“ go test ./... ”命令将能够获取它们。
But:但:

  • you will need to prefix your exported variables and functions (used in your tests) with the name of your package, in order for the test file to be able to access the package exported content.您需要在导出的变量和函数(在测试中使用)前加上包的名称,以便测试文件能够访问包导出的内容。
  • you wouldn't access non-exported content.你不会访问非导出的内容。

That being said, I would still prefer keep the _test.go file right beside the main source file: it is easier to find.话虽如此,我仍然希望将_test.go文件保留在主源文件旁边:它更容易找到。


For code coverage:对于代码覆盖率:

go test -coverpkg=./... ./...

See " How to plot Go test coverage over time " from Frédéric G. MARAND and fgmarand/gocoverstats to produce aggregate coverage statistics for CI integration of Go projects.请参阅Frédéric G. MARANDfgmarand/gocoverstats中的“如何绘制随时间变化的 Go 测试覆盖率”,为 Go 项目的 CI 集成生成聚合覆盖率统计信息。

Also go-cover-treemap.io is fun. go-cover-treemap.io也很有趣。

EDITED已编辑

Built on VonC's answer,基于 VonC 的回答,

This answer is valid in go1.11 .这个答案在go1.11中有效。 No yet tested in upper go versions.尚未在更高版本中go测试。

For those of you who like to keep their tests in a sub-folder, say test , then running对于那些喜欢将测试保存在子文件夹中的人,说test ,然后运行

go test ./...

will attempt to run tests in every folder, even those that do not contain any test, thus having a ?将尝试在每个文件夹中运行测试,即使是那些不包含任何测试的文件夹,因此会有? in the subsequent report for non-test folders.在非测试文件夹的后续报告中。

Running跑步

go test ./.../test

instead will target only your test folders, thus having a clean report focused on your tests folders only.而是只针对您的test文件夹,因此有一个干净的报告只关注您的测试文件夹。

CAUTION警告

Please be mindful that using test sub-folders will prevent coverage report computation.请注意,使用测试子文件夹会阻止覆盖率报告计算。 The phylosophy of go is to leave test files in the package folders. Go 的原理是将测试文件留在包文件夹中。

Put your tests alongside your code in the same directory in a file called file_test.go where "file" is the name of the source code file you're testing.将您的测试与您的代码一起放在名为file_test.go的文件中的同一目录中,其中“file”是您正在测试的源代码文件的名称。 This is convention and I've found it to be best in my own experience.这是惯例,我发现它在我自己的经验中是最好的。

If the go test tool isn't quite automated enough for you, you might look into GoConvey , which has a web UI that will automatically update and run traditional Go tests as well as GoConvey tests (which are based on behavior, and are more self-documenting than traditional Go tests).如果go test工具对您来说不够自动化,您可以查看GoConvey ,它有一个 Web UI,可以自动更新和运行传统 Go 测试以及 GoConvey 测试(基于行为,并且更自我) - 记录比传统的 Go 测试)。

I normally don't do test, but you can group your file into directories and use import like我通常不做测试,但你可以将你的文件分组到目录中并使用 import like

import "./models" if is one level out import "./models"如果是一级
import "../models if is one level out and one level in import "../models如果是一级输出和一级输入

For example, for:例如,对于:
./models/todo.go
./test/todo_test.go

to test todo.go from todo_test.go , your import in the todo_test.go will be要从todo_test.go测试todo.go ,您在todo_test.go中的导入将是

import "../models"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM