简体   繁体   中英

How do I dynamically skip tests in golang?

We have a special text file on an HTTP server that contains the filenames and test functions we want to skip when our golang tests run.

I must build something which downloads that test file, parses the file names and test functions that should be skipped, and then finally runs our go tests and properly skips the test functions found in the input file.

What's the right way to make this work in golang?

(I realize this sounds like an unusual way to skip, but we really want to make this work as I have described for reasons which are out of context to this question.)

You can skip test cases with (*testing.T).Skip() function. You can download the test file in the init function of go's test file. Then parse and load the function names in map or array. Before running each case, check if the test case is included in the skip list and skip if required.

// Psuedo code -> foo_test.go
var skipCases map[string]bool

func init() {
    // download and parse test file.
    // add test case names which needs skipped into skipCases map
}

func TestFoo(t *testing.T) {
    if skipCases["TestFoo"] {
        t.Skip()
    }
    // else continue testing
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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