I tend to do this using a helper, such as func connectToDB(t *testing.T) *postgres.DB { t.Helper() // set up the connection, using t.Fatalf if an error occurs return conn }
func UserTest(t *testing.T) { db := connectToDB(t) } This doesn't seem significantly worse in terms of convenience. But it has the advantage of being easily readable and only using the regular go tooling without any shenanigans. On Wed, Nov 3, 2021 at 9:33 PM Matt Mueller <mattmue...@gmail.com> wrote: > I'm looking for a way to inject test dependencies into a test suite. > Something like: > > ``` > package user_test > > func UserTest(t *testing.T, db *postgres.DB) { > fmt.Println("running user test", New("Alice Wonderland")) > } > ``` > > ``` > go_test ./user_test.go > ``` > > Where go_test would either manipulate the file in memory and pass it to go > test, or wrap it somehow in a separate go test file someplace else. I'm > trying to make this as transparent to the developer as possible. Just like > how go test works. > > Any ideas? Thanks! > > Matt > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/7ef4b9e0-3591-4505-9ce8-2bb9a7e76fe6n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/7ef4b9e0-3591-4505-9ce8-2bb9a7e76fe6n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGpaKp2cnysTQTOnTzr257CAG-FevE2chGu-2wbj1yxxQ%40mail.gmail.com.