如何传递参数来运行测试代码?

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

How do I pass arguments to run the test code

问题

我有两个文件main.go和main_test.go

在main.go中:

package main

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

func Sum(a, b int) int {
	return a + b
}

func main() {
	a, _ := strconv.Atoi(os.Args[1])
	b, _ := strconv.Atoi(os.Args[2])

	fmt.Println(Sum(a, b))
}

在main_test.go中:

package main

import (
	"flag"
	"fmt"
	"testing"
)

func TestMain(t *testing.M) {
	args1 := flag.Arg(0)
	args2 := flag.Arg(1)

	fmt.Print(args1, args2)

	os.Args = []string{args1, args2}

	t.Run()
}

我正在尝试通过go test main_test.go -args 1 2 -v来运行go测试,但是输出结果不正确。有人可以指导我如何编写命令来正确测试main函数吗?

英文:

I have two files main.go and main_test.go

under main.go

package main

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

func Sum(a, b int) int {
	return a + b
}

func main() {
	a, _ := strconv.Atoi(os.Args[1])
	b, _ := strconv.Atoi(os.Args[2])

	fmt.Println(Sum(a, b))
}


and under main_test.go I have

package main

import (
	"flag"
	"fmt"
	"testing"
)

func TestMain(t *testing.M) {
    args1 := flag.Arg(0)
	args2 := flag.Arg(1)

	fmt.Print(args1, args2)

	os.Args = []string{args1, args2}

	t.Run()


}


I am trying to run the go test by go test main_test.go -args 1 2 -v but I am not getting the output correct can anybody guide me how to write the command for the testing the main function so that it runs properly.

答案1

得分: 0

这是用于测试主函数的代码,其中传递了参数:

func TestMain(m *testing.M) {
    a := make([]string, len(os.Args)+2)
    a[0] = os.Args[0]
    a[1] = "第一个参数"
    a[2] = "第二个参数"
    copy(a[3:], os.Args[1:])

    os.Args = a

    main()
}

请注意,这段代码是用于测试目的,不是完整的程序。它创建了一个字符串切片 a,并将参数值分配给其中的元素。然后,将 os.Args 设置为新的切片 a,最后调用 main() 函数。

英文:

here is the code to test the main function with arguments passed to it:

func TestMain(m *testing.M) {
    a := make([]string, len(os.Args)+2)
    a[0] = os.Args[0]
    a[1] = "first arg"
    a[2] = "second arg"
    copy(a[3:], os.Args[1:])

    os.Args = a

    main()
}

答案2

得分: -1

一个使用固定示例(测试用例)的测试比在测试中使用任何交互式内容更有效,因为你可能需要多次自动运行它。为什么你不像我的示例那样做呢?

main_test.go:

package main

import (
	"testing"
)

func TestMain(t *testing.T) {

	for _, testCase := range []struct {
		a, b, result int
	}{
		{1, 2, 3},
		{5, 6, 11},
		{10, 2, 12},
	} {
		if Sum(testCase.a, testCase.b) != testCase.result {
			t.Fail()
		}
	}

}

这个例子也很好参考一下:
Go By Example: Testing

英文:

A Test with constant examples (test cases) would be more productive than using any interactive things in your tests because you might need it to run many times and automatically. Why aren't you doing it like my example?

main_test.go:

package main

import (
	"testing"
)

func TestMain(t *testing.T) {

	for _, testCase := range []struct {
		a, b, result int
	}{
		{1, 2, 3},
		{5, 6, 11},
		{10, 2, 12},
	} {
		if Sum(testCase.a, testCase.b) != testCase.result {
			t.Fail()
		}
	}

}

It's good to have a look at this example too:
Go By Example: Testing

huangapple
  • 本文由 发表于 2022年1月10日 15:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/70649121.html
匿名

发表评论

匿名网友

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

确定