如何在Golang中测试库函数

huangapple go评论80阅读模式
英文:

How to test library functions in Golang

问题

我正在学习Go语言,所以我正在编写一些基本的工具和库,以供其他模块重用。

当在另一个模块中导入它们(例如使用go get命令),我可以正常使用它们。

据我了解,Go语言会查找一个名为"package main"和一个main函数作为入口点来运行Go应用程序/模块。

问题是这些库不在"package main"中,也不在main.go文件中,并且没有main函数,那么我该如何使用命令行界面并使用"go run ."来测试我的函数的行为呢?

我是否必须创建另一个带有"package main"的模块和一个main.go文件?
或者我需要创建一个测试文件并使用"go test"命令?

谢谢!

英文:

I'm learning Go so I'm writing some basic utils and libraries for fun that I can reuse in other modules.

When importing them inside another module (using go get for example), I can use them just fine.

As I understand, Go will look for a package main and a main function to know the entrypoint when trying to run a go app/module.

The thing is that the libraries are not in "package main", not in main.go and have no main function, so how am I supposed to use the cli and use go run . to test the behavior of my function?

Do I absolutely need to create another module with a main.go file inside a package main?
Or do I need to create a test file and use go test ?

Thank you!

答案1

得分: 4

非主要包(即库包)不可运行。

要运行其中的代码,您必须运行一个导入并运行其代码的主要包,或者在_test.go文件中编写测试函数来运行代码。请注意,测试文件不仅仅用于单元测试;您可以在其中编写任何内容,并且通常比创建一个新的主要包进行测试更容易。

现代IDE现在可以让您轻松地在调试模式下运行单个测试,这样您就可以逐步执行代码并查看其是否按预期工作。如果您更喜欢使用打印调试并从命令行执行所有操作,您仍然可以运行单个测试并查看打印输出,如下所示:

// something_test.go
package mylibrary

func TestSomeFunction(t *testing.T) {
	SomeFunction()
}
go test mylibrary -v -run TestSomeFunction
英文:

Non-main packages (i.e., library packages) are not runnable.

To run the code in them, you must either run a main package that imports and runs their code, or write test functions in _test.go files to run the code. Note that test files are not just for unit tests; you can write whatever you want in them, and they can often be easier than creating a new main package for testing.

Modern IDEs now will allow you to easily run individual tests in debugging mode, so you can step through your code and see if it's working how you expect. If you prefer print debugging and doing everything from the command-line, you can still run an individual test and see the print output like this:

// something_test.go
package mylibrary

func TestSomeFunction(t *testing.T) {
	SomeFunction()
}
go test mylibrary -v -run TestSomeFunction

答案2

得分: 3

go test 是测试 Go 代码的首选方式。

如果出于某种原因不想使用 go test,可以使用 build constraintsgo run 命令来执行与包位于同一目录中的主程序。

下面是一个带有构建约束的示例程序。将该文件放置在与包源文件相同的目录中。注释 //go:build ignore 是一个构建约束,它将该文件从包源中排除。

//go:build ignore

package main

import (
	"your/package/import/path"  // <-- 修改为你的路径
    "fmt"
)

func main() {
     // 在这里使用你的包做一些事情。
     fmt.Println("example")
}

假设上述文件名为 program.go,可以使用以下命令在包目录中运行该程序:

go run program.go

go run 命令适用于运行类似这样的玩具或测试程序。对于其他任何情况,请优先使用 go build 命令。

英文:

go test is the preferred way to test Go code.

If you do not want to use go test for some reason, then use build constraints and the go run command to execute a main program from the same directory as your package.

Here's an example program with a build constraint. Place the file in the same directory as the package source files. The comment //go:build ignore is a build constraint that excludes the file from the package sources.

//go:build ignore

package main

import (
	&quot;your/package/import/path&quot;  // &lt;-- modify to your path
    &quot;fmt&quot;
)

func main() {
     // do something with your package here.
     fmt.Println(&quot;example&quot;)
}

Assuming that the above file is named program.go, run the program from the package directory with the command:

go run program.go

The go run command is OK for running toy or test programs like this. Prefer the go build command for anything else.

huangapple
  • 本文由 发表于 2023年1月14日 06:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75115136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定