首页 > 后端开发 > Golang > 正文

我应该使用子目录进行 Go 测试吗?

Barbara Streisand
发布: 2024-11-14 17:19:01
原创
686 人浏览过

Should I Use Sub-Directories for Go Tests?

Sub-Directories for Go Tests: Keeping Your Workspace Organized

In Go, it's common to keep tests in the same directory as the code they're testing. However, for improved organization, you may want to create separate sub-directories for your package, tests, and examples.

Running Tests Recursively

To run tests in sub-directories, you can use the go test command with the ./... notation:

go test ./...
登录后复制

This recursively lists all packages in your project and runs their tests.

Accessing Exported Content

If you keep your test files in a sub-directory, you must prefix exported variables and functions with the package name to allow the test file to access them.

Code Coverage

For code coverage, you can use:

go test -coverpkg=./... ./...
登录后复制

Integration Tests

Since Go 1.20, you can use go-cover to collect profiles from larger integration tests.

Separate Test Package

Alternatively, you can put your tests in a separate package without creating a sub-directory. For example, tests for package foo can be placed in package foo_test. This allows you to keep tests separate while still ensuring access to exported content.

以上是我应该使用子目录进行 Go 测试吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板