797 words
4 minutes

Go语言教程:单元测试与命令行参数

本文将通过一个示例代码,详细介绍Go语言中的单元测试和命令行参数的使用。

示例代码解析#

代码结构#

示例代码分为两个部分:

  1. 单元测试部分
  2. 主程序部分

单元测试部分#

单元测试部分位于main_test.go文件中,包含三个测试函数:TestHelloTestIntroduceTestFail

TestHello函数#

func TestHello(t *testing.T) {
var name string
greeting, err := hello(name)
if err == nil {
t.Errorf("The error is nil, but it should not be. (name=%q)", name)
}
if greeting != "" {
t.Errorf("Nonempty greeting, but it should not be. (name=%q)", name)
}
name = "Robert"
greeting, err = hello(name)
if err != nil {
t.Errorf("The error is not nil, but it should be. (name=%q)", name)
}
if greeting == "" {
t.Errorf("Empty greeting, but it should not be. (name=%q)", name)
}
expected := fmt.Sprintf("Hello, %s!", name)
if greeting != expected {
t.Errorf("The actual greeting %q is not the expected. (name=%q)", greeting, name)
}
t.Logf("The expected greeting is %q.\\n", expected)
}

解析:

  • 测试hello函数的行为。
  • name为空时,期望返回错误和空字符串。
  • name为”Robert”时,期望返回正确的问候语。

TestIntroduce函数#

func TestIntroduce(t *testing.T) {
intro := introduce()
expected := "Welcome to my Golang column."
if intro != expected {
t.Errorf("The actual introduce %q is not the expected.", intro)
}
t.Logf("The expected introduce is %q.\\n", expected)
}

解析:

  • 测试introduce函数的行为。
  • 期望返回固定的介绍语。

TestFail函数#

func TestFail(t *testing.T) {
//t.Fail()
t.FailNow() // 此调用会让当前的测试立即失败。
t.Log("Failed.")
}

解析:

  • 演示如何使测试失败。
  • t.FailNow()会立即终止测试。

主程序部分#

主程序部分位于main.go文件中,包含initmainhellointroduce函数。

init函数#

func init() {
flag.StringVar(&name, "name", "everyone", "The greeting object.")
}

解析:

  • 使用flag包定义命令行参数name,默认值为”everyone”。

main函数#

func main() {
flag.Parse()
greeting, err := hello(name)
if err != nil {
fmt.Printf("error: %s\\n", err)
return
}
fmt.Println(greeting, introduce())
}

解析:

  • 解析命令行参数。
  • 调用hello函数生成问候语。
  • 调用introduce函数生成介绍语。
  • 输出结果。

hello函数#

func hello(name string) (string, error) {
if name == "" {
return "", errors.New("empty name")
}
return fmt.Sprintf("Hello, %s!", name), nil
}

解析:

  • 根据传入的name生成问候语。
  • 如果name为空,返回错误。

introduce函数#

func introduce() string {
return "Welcome to my Golang column."
}

解析:

  • 返回固定的介绍语。

最佳实践#

  1. 单元测试
    • 使用testing包编写单元测试。
    • 测试函数命名以Test开头。
    • 使用t.Errorf报告错误,使用t.Logf记录日志。
  2. 命令行参数
    • 使用flag包解析命令行参数。
    • init函数中定义命令行参数。
  3. 错误处理
    • 函数返回错误时,优先处理错误。
    • 使用errors.New创建错误信息。

举一反三#

  1. 扩展问候语功能
    • 可以增加更多的问候语选项,例如根据时间段问候(早上好、下午好、晚上好)。
  2. 增加更多命令行参数
    • 可以增加更多的命令行参数,例如agelocation,并在问候语中使用这些参数。
  3. 改进错误处理
    • 可以使用自定义错误类型,提供更详细的错误信息。

通过以上示例和解析,相信你已经掌握了Go语言中单元测试和命令行参数的基本用法。希望你能在实际项目中灵活运用这些知识,编写出高质量的Go代码。

Share

If this article helped you, please share it with others!

Go语言教程:单元测试与命令行参数
https://blog.ithuo.net/posts/go-unit-testing-and-command-line-arguments-tutorial/
Author
Derick
Published at
2022-05-21
License
CC BY-NC-SA 4.0
Last updated on 2022-05-21,1348 days ago

Some content may be outdated

Table of Contents