Home > Backend Development > Golang > Should I Use Sub-Directories for Go Tests?

Should I Use Sub-Directories for Go Tests?

Barbara Streisand
Release: 2024-11-14 17:19:01
Original
767 people have browsed it

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 ./...
Copy after login

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=./... ./...
Copy after login

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.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template