2

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.)

2
  • 4
    (1) Perform the HTTP download and parsing in the TestMain function, then (2) write a function that calls t.Skip()if the test should be skipped, and invoke that from each Test* function. This function can get the current call stack to figure out whether the current test should be skipped. Commented Dec 5, 2018 at 13:01
  • @TimCooper There are thousands of test files in my case. Can I do this globally like you can in Python or PHP once? Your answer suggests I have to apply this to all of the files with no ability to implement a single solution for all of the test suite. Commented Dec 6, 2018 at 0:13

1 Answer 1

7

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 } 
Sign up to request clarification or add additional context in comments.

2 Comments

Though the above is technically correct, I won't mark this one as accepted because the spirit of my problem is that I have thousands of test files. It is not viable to add this if condition to each test. Is there some way that this can be applied to the entire test suite just once and have it apply to all of the tests? (I do very much appreciate the responses)
You can also try "-run regexp" flag for go test command. You can use naming convention to group your test cases and pass regex while running the test case to execute test cases matching specific pattern. But none of these solutions will work without changing you existing test cases.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.