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.
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.
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.
For code coverage, you can use:
go test -coverpkg=./... ./...
Since Go 1.20, you can use go-cover to collect profiles from larger integration tests.
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中文网其他相关文章!