The testing package in Go provides a TB (testing.TB) interface that can be used to run and manage unit tests. It provides methods for logging and reporting test results. One of the methods provided by the TB interface is the Skip method which can be used to skip a test.
Example 1: In this example, we are testing a function that calculates the sum of two numbers. We are using the TB's Skip method to skip a test case that we know will fail. This is useful in cases where we know that a certain scenario will fail, but we don't want to fail the entire test suite.
func TestSum(t *testing.T) { cases := []struct{ a, b, expected int }{ {1, 2, 3}, {3, 4, 7}, {10, -5, 5}, {100, -100, 0}, {0, 0, 0}, {1, 1000, 0}, // This test case will be skipped }
for _, c := range cases { if c.a+c.b != c.expected { t.Errorf("Sum(%d,%d) == %d, expected %d", c.a, c.b, c.a+c.b, c.expected) } }
t.Skip("Skipping test case: Sum(1,1000)") }
Package: testing
Example 2: In this example, we are testing a function that manipulates a string. We're using the TB's SkipNow method to immediately skip the test when a certain condition is met. This is useful in cases where we don't want to run other tests if we know that a critical condition has not been met.
if str == "" { t.SkipNow() // Skipping test case, empty string }
res := manipulateString(str)
if res != "gnirts tset" { t.Errorf("manipulateString(%s) = %s, expected: gnirts tset", str, res) } }
Package: testing
In both examples, we are using the testing package's TB interface methods to skip a test case. The first example skips a test case using the Skip method, while the second example skips a test case immediately using the SkipNow method. The package used in both examples is the testing package.
Golang TB.Skip - 26 examples found. These are the top rated real world Golang examples of testing.TB.Skip extracted from open source projects. You can rate examples to help us improve the quality of examples.