|
1 | | -# go-assert |
| 1 | +# Assertion for Golang |
| 2 | + |
| 3 | + |
| 4 | +[](https://codecov.io/gh/ghosind/go-assert) |
| 5 | + |
| 6 | + |
| 7 | +[](https://pkg.go.dev/github.com/ghosind/go-assert) |
| 8 | + |
| 9 | +A collection of Golang assertion functions for verifying invariants. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +To install this library, just use `go get` command like the following line: |
| 14 | + |
| 15 | +```bash |
| 16 | +go get -u github.com/ghosind/go-assert |
| 17 | +``` |
| 18 | + |
| 19 | +## Getting Started |
| 20 | + |
| 21 | +This library provided assertion functions to verify the equality of values: |
| 22 | + |
| 23 | +```go |
| 24 | +func TestExample(t *testing.T) { |
| 25 | + // assert equality |
| 26 | + assert.DeepEqual(t, actual, expect) |
| 27 | + |
| 28 | + // assert inequality |
| 29 | + assert.NotDeepEqual(t, actual, expect) |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +It also provided assertion functions to verify a function will panic or not: |
| 34 | + |
| 35 | +```go |
| 36 | +func TestPanic(t *testing.T) { |
| 37 | + // assert panic |
| 38 | + assert.Panic(t, func () { |
| 39 | + // do something |
| 40 | + |
| 41 | + panic() |
| 42 | + }) |
| 43 | + |
| 44 | + // assert no panic |
| 45 | + assert.NotPanic(t, func () { |
| 46 | + // do something |
| 47 | + |
| 48 | + // panic() |
| 49 | + }) |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Every assertion will not terminate the testing workflow. However, they'll return an error if the verification failed, and you can check the return value to get the verification result. |
| 54 | + |
| 55 | +```go |
| 56 | +func TestExample(t *testing.T) { |
| 57 | + if err := assert.DeepEqual(t, actual, expect); err != nil { |
| 58 | + // terminate test |
| 59 | + t.Fail() |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +If you need to assert many times, you can also create an `Assertion` instance: |
| 65 | + |
| 66 | +```go |
| 67 | +func TestExample(t *testing.T) { |
| 68 | + assertion := assert.New(t) |
| 69 | + |
| 70 | + // test equality |
| 71 | + assertion.DeepEqual(actual, expect) |
| 72 | + |
| 73 | + // Test inequality |
| 74 | + assertion.NotDeepEqual(actual, expect) |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +This project was published under the MIT license, you can see [LICENSE](./LICENSE) file to get more information. |
0 commit comments