gotestifygo-testinggo-flag

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


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

Solution

  • 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