Home  >  Article  >  Backend Development  >  Can I Organize Go Tests in Sub-Directories?

Can I Organize Go Tests in Sub-Directories?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-19 11:26:02896browse

Can I Organize Go Tests in Sub-Directories?

Golang Tests in Sub-Directories: Is It Possible and How?

Can I Organize Tests in Sub-Directories in Go?

Yes, you can organize tests in sub-directories in Go. This allows you to keep your workspace cleaner and organize your tests more effectively.

How to Organize Tests in Sub-Directories

To organize tests in sub-directories, create a sub-directory named "tests" or "test" within the package directory. Place your test files in this sub-directory.

Running Tests in Sub-Directories

To run tests in sub-directories, use the following command:

go test ./...

The ./... notation specifies that all packages and their sub-directories should be tested.

Note:

  • You need to prefix exported variables and functions in your test files with the package name to access the package's exported content.
  • Non-exported content cannot be accessed from test files in sub-directories.

Alternative Considerations

While organizing tests in sub-directories can be beneficial, some prefer to keep test files alongside the main source files for easier access. Additionally, with Go 1.20, coverage tooling now supports collecting profiles from larger integration tests.

Conclusion

Organizing tests in sub-directories in Go is possible and offers flexibility in test organization. However, the choice depends on individual preferences and the specific needs of your project.

The above is the detailed content of Can I Organize Go Tests in Sub-Directories?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn