An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
June 29, 2021 06:55 pm GMT
Original Link: https://dev.to/jonasbn/til-skipping-tests-in-go-3i5l
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
Lifted from my TIL collection
Original Link: https://dev.to/jonasbn/til-skipping-tests-in-go-3i5l
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To