Patutkah Saya Menggunakan Sub-Direktori untuk Ujian Go?

Barbara Streisand
Lepaskan: 2024-11-14 17:19:01
asal
686 orang telah melayarinya

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 ./...
Salin selepas log masuk

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=./... ./...
Salin selepas log masuk

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.

Atas ialah kandungan terperinci Patutkah Saya Menggunakan Sub-Direktori untuk Ujian Go?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan