golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.96k stars 17.66k forks source link

testing: add `assert` function #21015

Closed jmeridth closed 6 years ago

jmeridth commented 7 years ago

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.8.3

What operating system and processor architecture are you using (go env)?

GOARCH="amd64" GOHOSTOS="linux" Ubuntu 17.04

I'd like to make the func assert available in the testing standard library package. I see it being used in multiple places already and it makes sense to centralize it and DRY up the usage.

Examples: https://github.com/golang/go/blob/master/test/const.go#L39 https://github.com/golang/go/blob/master/test/iota.go#L11 https://github.com/golang/go/blob/master/test/typeswitch.go#L38

bradfitz commented 7 years ago

I don't think we'd do this before Go had generics (#15292) even if we did. I don't know what's changed from previous times when this has been rejected. (usual argument I see is that testing assertion libraries become little DSLs and medium DSLs and then it's a whole new language you're using instead of using the language that the rest of your program is written in)

I'm going to place this on hold until #15292 makes some progress, though.

In the meantime you can use https://github.com/stretchr/testify etc.

jmeridth commented 7 years ago

@bradfitz I'm new to the language and looked through the current issues. I should have looked through the closed ones.:smile: example

Thank you for the polite and informational response. I look forward to seeing where this goes.

Cheers :beers:

bradfitz commented 7 years ago

Right, thanks. I forgot this was in the FAQ, as you've found: https://golang.org/doc/faq#testing_framework

jmeridth commented 7 years ago

@bradfitz ah, thanks. I shall read all of that before filing future issues. :smile:

bronze1man commented 7 years ago

I also think *test.Testing is too complex to use. Why not just use panic when something is wrong? Understand stack trace has less job then write right error message.

I think following function prototypes are better than the one is suggested.

func Assert(t bool, s ...string)  
func AssertEqual(a interface{},b interface{},s ...string) // a and b should have the same json.Marshal output.

https://github.com/bronze1man/kmg/blob/master/kmgTest/testV2.go#L11

ianlancetaylor commented 6 years ago

Nothing to do here.