| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 1 | // Package assert provides a set of comprehensive testing tools for use with the normal Go testing system. |
| 2 | // |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 3 | // # Note |
| 4 | // |
| 5 | // All functions in this package return a bool value indicating whether the assertion has passed. |
| 6 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 7 | // # Example Usage |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 8 | // |
| 9 | // The following is a complete example using assert in a standard test function: |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 10 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 11 | // import ( |
| 12 | // "testing" |
| 13 | // "github.com/stretchr/testify/assert" |
| 14 | // ) |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 15 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 16 | // func TestSomething(t *testing.T) { |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 17 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 18 | // var a string = "Hello" |
| 19 | // var b string = "Hello" |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 20 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 21 | // assert.Equal(t, a, b, "The two words should be the same.") |
| 22 | // |
| 23 | // } |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 24 | // |
| 25 | // if you assert many times, use the format below: |
| 26 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 27 | // import ( |
| 28 | // "testing" |
| 29 | // "github.com/stretchr/testify/assert" |
| 30 | // ) |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 31 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 32 | // func TestSomething(t *testing.T) { |
| 33 | // assert := assert.New(t) |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 34 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 35 | // var a string = "Hello" |
| 36 | // var b string = "Hello" |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 37 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 38 | // assert.Equal(a, b, "The two words should be the same.") |
| 39 | // } |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 40 | // |
| Akash Reddy Kankanala | d9ec482 | 2025-06-10 22:59:53 +0530 | [diff] [blame] | 41 | // # Assertions |
| kesavand | b9f54fd | 2021-11-25 20:08:04 +0530 | [diff] [blame] | 42 | // |
| 43 | // Assertions allow you to easily write test code, and are global funcs in the `assert` package. |
| 44 | // All assertion functions take, as the first argument, the `*testing.T` object provided by the |
| 45 | // testing framework. This allows the assertion funcs to write the failings and other details to |
| 46 | // the correct place. |
| 47 | // |
| 48 | // Every assertion function also takes an optional string message as the final argument, |
| 49 | // allowing custom error messages to be appended to the message the assertion method outputs. |
| 50 | package assert |