smartystreets / goconvey

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
http://smartystreets.github.io/goconvey/
Other
8.23k stars 554 forks source link

ShouldResemble for proto messages #664

Open antoniomacri opened 2 years ago

antoniomacri commented 2 years ago

Comparison of proto Messages performed using ShouldResemble is unreliable.

Consider this code:

package main

import (
    . "github.com/smartystreets/goconvey/convey"
    "google.golang.org/protobuf/types/known/wrapperspb"
    "log"
    "testing"
)

func TestConveyBug(t *testing.T) {
    Convey("Given something", t, func() {
        var x = &wrapperspb.BoolValue{Value: false}
        var y = &wrapperspb.BoolValue{Value: false}
        log.Printf("Receive type %v", x)

        So(x, ShouldResemble, y)
    })
}

The call to Printf causes x.String() to be called. This in turn initializes the state and its atomicMessageInfo field on the proto message. Therefore x has the atomicMessageInfo set, while y doesn't. This causes reflect.DeepEqual to return false.

Another problem stems from the rendering of the diff (#660).

Could a ShouldResembleProto be added for expliciting comparing proto messages? It should call proto.Equal.

srabraham commented 1 year ago

Hey Antonio, note that you can define your own ShouldResembleProto and use that with GoConvey. That's exactly what the Chrome infrastructure team has done, as they're big users of GoConvey and of Protocol Buffers. I'd be curious for @riannucci to chime in, but it might be weird for GoConvey to pull in the protobuf repo as a dependency to make this natively supported.

Have a look at these: definition of ShouldResembleProto: https://chromium.googlesource.com/infra/luci/luci-go/+/3809e3303d0b/common/testing/assertions/proto_tests.go#37 example usage of that function: https://chromium.googlesource.com/infra/luci/luci-go/+/3809e3303d0b/auth_service/impl/servers/accounts/server_test.go#43