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