smarty / gunit

xUnit-style test fixture adapter for go test
Other
119 stars 11 forks source link

Mechanism for assertions to signal immediate/fatal termination of failed test cases #33

Open mdwhatcott opened 11 months ago

mdwhatcott commented 11 months ago

...via *testing.T.Fatalf.

This change is meant to be released in concert with https://github.com/smarty/assertions/pull/55, which introduces a new must package.

This new feature addresses cases where an assertion failure early in a test should kill a test immediately. Here are a few examples of how we've approached this up until this PR:

Length checks before assertions:

Old way:

if !this.So(collection, should.HaveLength, 2) {
    return // we don't have enough items in the collection for upcoming assertions
}
this.So(collection[1].Whatever, should.Equal, "something")
... // more assertions may follow

New way:

this.So(collection, must.HaveLength, 2) // <- new 'must' package ends failed test immediately
this.So(collection[1].Whatever, should.Equal, "Something")

Error checking before subsequent assertions:

Old way:

value, err := SomeOperationThatCouldFail()
if !this.So(err, should.BeNil) {
    return
}
this.So(value, should.Equal, 42)

New way:

value, err := SomeOperationThatCouldFail()
this.So(err, must.BeNil) // <- new 'must' package ends failed test immediately
this.So(value, should.Equal, 42)