Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 29, 2021 06:55 pm GMT

TIL: Skipping Tests in Go

The Go testing framework and toolchain has a nifty facility, where you can skip tests, if you do not want them to be a part of every run.

Let say we have a basic implementation

package shorttestimport (    "fmt"    "time")func DoUnimportantStuff() uint8 {    fmt.Println("Doing unimportant stuff")    time.Sleep(10 * time.Second)    return 1}func DoImportantStuff() uint8 {    fmt.Println("Doing important stuff")    return 1}

And we have a corresponding test suite:

package shorttestimport "testing"func TestImportant(t *testing.T) {    got := DoImportantStuff()    if got != 1 {        t.Errorf("Important stuff not correct, needed %d", got)    }}func TestUnimportant(t *testing.T) {    if testing.Short() {        t.Skip("skipping test in short mode.")    } else {        got := DoUnimportantStuff()        if got != 1 {            t.Errorf("Unimportant stuff not correct, needed %d", got)        }    }}

When we are developing and want fast feedback, we do not want to wait for the long running and unimportant test to finish, but we are VERY interested in getting feedback on our important function as fast as possible.

When we just test we will observe waiting time

$ go testDoing important stuffDoing unimportant stuffPASSok      shorttest   10.364s

We can then skip the execution of the unimportant tests by executing our test suite with the --short flag.

 go test --shortDoing important stuffPASSok      shorttest   0.116s

We just have to remember to add the handling of --short via testing.Short() in the non-critical and long running tests.

Resources and References

  1. Go Command: Testing Flags
  2. Go Package testing: Skipping

Lifted from my TIL collection


Original Link: https://dev.to/jonasbn/til-skipping-tests-in-go-3i5l

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To