如何在测试中使用自定义标志(使用`testify/suite`)

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

How to use custom flag in tests (with `testify/suite`)

问题

我想为使用testify/suite的Go测试添加自定义标志。根据这个线程,似乎只能在TestMain()中添加(如果在Go 1.13之前,则是在init()中)。然而,对于testify/suite包来说,TestMain()不太适用。我尝试在SetupSuite()TestMyTestSuite()中声明标志,这两个函数似乎对应于TestMain(),但都返回错误flag provided but not defined: -mycustomflag。以下是示例代码。任何建议将不胜感激!

my_test.go:

package main

import (
	"flag"
	"fmt"
	"github.com/stretchr/testify/suite"
	"testing"
)

type MyTestSuite struct {
	suite.Suite
}

func (suite *MyTestSuite) SetupSuite() {
	flagBoolPtr := flag.Bool("mycustomflag", false, "this is a bool flag")
	flag.Parse()
	fmt.Printf("my flag is set to: %t", *flagBoolPtr)
}

func TestMyTestSuite(t *testing.T) {
	// flagBoolPtr := flag.Bool("mycustomflag", false, "this is a bool flag")
	// flag.Parse()
	// fmt.Printf("my flag is set to: %t", *flagBoolPtr)
	suite.Run(t, new(MyTestSuite))
}

func (suite *MyTestSuite) TestBuildClosure() {
	fmt.Println("my test")
}

这是我使用的命令:

go test my_test.go -mycustomflag
英文:

I want to add custom flags for my Go tests that use testify/suite. It looks like from this thread that it can only be in TestMain() (init() if it is before Go 1.13). However, with the testify/suite pacakge, TestMain() is not quite an option. I have tried declaring the flags in SeupSuite() and TestMyTestSuite() which seems to be a corresponding TestMain() but both returned the error flag provided but not defined: -mycustomflag. Below is the sample code. Any suggestion will be appreciated!

my_test.go:

package main

import (
	"flag"
	"fmt"
	"github.com/stretchr/testify/suite"
	"testing"
)

type MyTestSuite struct {
	suite.Suite
}

func (suite *MyTestSuite) SetupSuite() {
	flagBoolPtr := flag.Bool("mycustomflag", false, "this is a bool flag")
	flag.Parse()
	fmt.Printf("my flag is set to: %t", *flagBoolPtr)
}

func TestMyTestSuite(t *testing.T) {
	// flagBoolPtr := flag.Bool("mycustomflag", false, "this is a bool flag")
	// flag.Parse()
	// fmt.Printf("my flag is set to: %t", *flagBoolPtr)
	suite.Run(t, new(MyTestSuite))
}

func (suite *MyTestSuite) TestBuildClosure() {
	fmt.Println("my test")
}

This is the command I used:

go test my_test.go -mycustomflag

答案1

得分: 0

go test生成的测试二进制文件已经在内部使用了flag包,并在正常运行期间调用了flag.Parse()。将标志变量定义为全局变量(✳️),以便在运行flag.Parse()之前就可以知道它们。

type MyTestSuite struct {
	suite.Suite
}

// ✳️
var flagBoolPtr = flag.Bool("mycustomflag", false, "this is a bool flag")

func (suite *MyTestSuite) SetupSuite() {
	fmt.Printf("my flag in SetupSuite: %t\n", *flagBoolPtr)
}

func TestMyTestSuite(t *testing.T) {
	fmt.Printf("my flag in test: %t\n", *flagBoolPtr)
	suite.Run(t, new(MyTestSuite))
}

func (suite *MyTestSuite) TestBuildClosure() {
	fmt.Println("my test")
}
go test -v my_test.go -mycustomflag

=== RUN   TestMyTestSuite
my flag in test: true
my flag in SetupSuite: true
=== RUN   TestMyTestSuite/TestBuildClosure
my test
--- PASS: TestMyTestSuite (0.00s)
    --- PASS: TestMyTestSuite/TestBuildClosure (0.00s)
PASS

但是如果我有多个测试套件,即同一个包中的多个文件?我想在所有测试套件中使用这个标志怎么办?由于我们只能定义变量和标志一次,我如何确保在这个特定的测试套件中首先执行这个声明?

要在包中单独运行测试,请创建一个仅包含所需flag(✳️)的文件,并使用包含flags的文件编译单个test

.
├── my_test.go
├── other_my_test.go
└── flag_test.go // ✳️

👇 flag_test.go

package any

import "flag"

var flagBoolPtr = flag.Bool("mycustomflag", false, "this is a bool flag")
go test -v flag_test.go my_test.go -mycustomflag

您还可以使用所需的flag运行所有测试。

 go test -v ./... -mycustomflag
英文:

The test binary generated by go test is already using the flag package internally and calls flag.Parse() during normal operation.
Define the flag variable as global (✳️) so they are known before running flag.Parse().

type MyTestSuite struct {
	suite.Suite
}

// ✳️
var flagBoolPtr = flag.Bool("mycustomflag", false, "this is a bool flag")

func (suite *MyTestSuite) SetupSuite() {
	fmt.Printf("my flag in SetupSuite: %t\n", *flagBoolPtr)
}

func TestMyTestSuite(t *testing.T) {
	fmt.Printf("my flag in test: %t\n", *flagBoolPtr)
	suite.Run(t, new(MyTestSuite))
}

func (suite *MyTestSuite) TestBuildClosure() {
	fmt.Println("my test")
}

go test -v my_test.go -mycustomflag

=== RUN   TestMyTestSuite
my flag in test: true
my flag in SetupSuite: true
=== RUN   TestMyTestSuite/TestBuildClosure
my test
--- PASS: TestMyTestSuite (0.00s)
    --- PASS: TestMyTestSuite/TestBuildClosure (0.00s)
PASS

> Although what if I have multiple testsuites aka multiple files in the same package? And I want to use this flag for all testsuites? Since we can define the variable and the flag only once, how can I make sure this declaration in this specific testsuite gets executed first?

To run test separately in the package, create a file for the package contains only with required flag (✳️) and compile single test with file, which contains flags

.
├── my_test.go
├── other_my_test.go
└── flag_test.go // ✳️

👇🏻 flag_test.go

package any

import "flag"

var flagBoolPtr = flag.Bool("mycustomflag", false, "this is a bool flag")
go test -v flag_test.go my_test.go -mycustomflag

also you can run all tests with required flag

 go test -v ./... -mycustomflag

huangapple
  • 本文由 发表于 2022年8月11日 03:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/73311561.html
匿名

发表评论

匿名网友

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

确定