jeudi 11 août 2016

Subset of table driven test

For testing functions I could select which will run by option -run.

go test -run regex

Very common if we have dozens test cases is put it into array in order not to write function for each of that:

cases := []struct {
  arg, expected string
} {
    {"%a", "[%a]"},
    {"%-a", "[%-a]"},
    // and many others
}
for _, c := range cases {
  res := myfn(c.arg) 
  if  res != c.expected {
    t.Errorf("myfn(%q) should return %q, but it returns %q", c.arg, c.expected, res)
  }
}

This work good, but problem is with maintanance. When I add a new testcase, while debugging I want to start just a new test case, but I cannot say something like:

go test -run TestMyFn.onlyThirdCase

Is there any elegant way, how to have many testcases in array together with ability to choose which testcase will run?

Aucun commentaire:

Enregistrer un commentaire