简体   繁体   English

在 go 中测试 CLI arguments

[英]Testing CLI arguments in go

I want to test some CLI routines in go that are run in the main() function purely, they are just exercises that I'm doing, but I want to make tests on them?我想纯粹测试 go 中运行在 main() function 中的一些 CLI 例程,它们只是我正在做的练习,但我想对它们进行测试? So for example how can I pass arguments to table test this king of algorithm?那么例如我如何通过 arguments 来测试这个算法之王?

package main


import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func main() {
    var f *os.File
    f = os.Stdin
    defer f.Close()

    scanner := bufio.NewScanner(f)

    for scanner.Scan() {
        if scanner.Text() == "STOP" || scanner.Text() == "stop" {
            break
        }

        n, err := strconv.ParseInt(scanner.Text(), 10, 64)

        if err == nil {
            fmt.Printf("Number formatted: %d\n", n)
        } else {
            fmt.Println(err.Error())
        }
    }
}

I put the code on playground too for better help!我也将代码放在操场上以获得更好的帮助! https://play.golang.org/p/JgrQ2yFogNs https://play.golang.org/p/JgrQ2yFogNs

Thanks in advance!提前致谢!

Strictly speaking, you don't need to use Go for this.严格来说,您不需要为此使用 Go。 When I'm looking at writing a CLI tool I break out of main as soon as possible with functions and structs the unit test those in the usual way.当我正在考虑编写一个 CLI 工具时,我会尽快使用函数打破main并以通常的方式构建单元测试。

To make sure that all the CLI arguments and file system buts are plumbed in correctly I've been using https://github.com/bats-core/bats-core to run the command and check the fully built tool.为了确保所有 CLI arguments 和文件系统都正确插入,我一直在使用https://github.com/bats-core/bats-core运行命令并检查完全构建的工具。

You can see an example of how I've done this at https://github.com/dnnrly/abbreviate您可以在https://github.com/dnnrly/abbreviate看到我如何做到这一点的示例

Perhaps not the answer you're looking for but it's been working well for me.也许不是您正在寻找的答案,但对我来说效果很好。

You need to create a function which input and output channels as params.您需要创建一个 function 输入和 output 通道作为参数。 It should read and write to these params.它应该读取和写入这些参数。 Following is an example:下面是一个例子:

main.go主.go

package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
    "strconv"
)

func main() {
    var f *os.File
    f = os.Stdin
    defer f.Close()
    run (os.Stdin, f)
}

func run(in io.Reader, out io.Writer) {
    scanner := bufio.NewScanner(in)

    for scanner.Scan() {
        if scanner.Text() == "STOP" || scanner.Text() == "stop" {
            break
        }

        n, err := strconv.ParseInt(scanner.Text(), 10, 64)

        if err == nil {
            fmt.Printf("Number formatted: %d\n", n)
        } else {
            fmt.Println(err.Error())
        }
    }
}

main_test.go main_test.go

package main

import (
    "bytes"
    "fmt"
    "testing"
)
func TestRun(t *testing.T){
    var command, result bytes.Buffer
    fmt.Fprintf(&command, "10\n")
    fmt.Fprintf(&command, "stop\n")
    run(&command, &result)
    got := result.String()
    //test for contents of "got"
    fmt.Println(got)
}

Now you can run the following on a command line.现在您可以在命令行上运行以下命令。

go test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM