| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 1 | // Package require implements the same assertions as the `assert` package but |
| 2 | // stops test execution when a test fails. |
| 3 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 4 | // # Example Usage |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 5 | // |
| 6 | // The following is a complete example using require in a standard test function: |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 7 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 8 | // import ( |
| 9 | // "testing" |
| 10 | // "github.com/stretchr/testify/require" |
| 11 | // ) |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 12 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 13 | // func TestSomething(t *testing.T) { |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 14 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 15 | // var a string = "Hello" |
| 16 | // var b string = "Hello" |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 17 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 18 | // require.Equal(t, a, b, "The two words should be the same.") |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 19 | // |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 20 | // } |
| 21 | // |
| 22 | // # Assertions |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 23 | // |
| 24 | // The `require` package have same global functions as in the `assert` package, |
| 25 | // but instead of returning a boolean result they call `t.FailNow()`. |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 26 | // A consequence of this is that it must be called from the goroutine running |
| 27 | // the test function, not from other goroutines created during the test. |
| amit.ghosh | 6ab2a98 | 2022-09-15 21:04:53 +0200 | [diff] [blame] | 28 | // |
| 29 | // Every assertion function also takes an optional string message as the final argument, |
| 30 | // allowing custom error messages to be appended to the message the assertion method outputs. |
| 31 | package require |