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.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!