--
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...@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.
func connectToDB(t *testing.T) *postgres.DB {t.Helper()// set up the connection, using t.Fatalf if an error occursreturn conn}func UserTest(t *testing.T) {db := connectToDB(t)}
Hey, thanks. I'm aware of this approach.I'm hoping for some technique that automatically injects, since it can be cumbersome to inject all your dependencies by hand.
Similar to google/wire, but without the generated file sitting in the filesystem next to your test.MattOn Wednesday, November 3, 2021 at 4:16:04 PM UTC-5 ben...@gmail.com wrote:func connectToDB(t *testing.T) *postgres.DB {t.Helper()// set up the connection, using t.Fatalf if an error occursreturn conn}func UserTest(t *testing.T) {db := connectToDB(t)}Yeah, that's a good way.And if you want to avoid re-connecting to the db every test, you can use a top-level test function for the "suite", and sub-tests with t.Run() for the individual tests, with the sub-tests closing over the db variable. Like so:func TestDatabaseThings(t *testing.T) {
db := connectToDB(t)
t.Run("Foo", func (t *testing.T) {
fmt.Println(db, "test foo stuff") // use "db" and test stuff
})
t.Run("Bar", func (t *testing.T) {
fmt.Println(db, "test bar stuff") // use "db" and test stuff
})
}-Ben
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2a237fed-ba58-436a-b8e8-5c5be3c5c47cn%40googlegroups.com.
On Wed, Nov 3, 2021 at 10:44 PM Matt Mueller <mattm...@gmail.com> wrote:Hey, thanks. I'm aware of this approach.I'm hoping for some technique that automatically injects, since it can be cumbersome to inject all your dependencies by hand.I don't understand what you mean. Obviously, how to connect to a database or other dependencies is highly application specific. So you'd still have to write both the code to create the connections and somehow mention what dependencies you need in the test.
Perhaps he's thinking of something like pytest. Simply by adding a named argument to your test function, a corresponding helper is called to create the value. There is a 'yield' variation so that the helper can also handle cleanup after the test; and helpers can invoke other helpers in the same way.@pytest.yield_fixture(scope='function')def session():db_session = make_session()yield db_sessiondb_session.close()@pytest.fixture(scope='function')def customer(session):c = Customer(name="Fred")session.add(c)return c# The actual testsdef test_foo(session, customer):assert session is not Noneassert customer is not Nonedef test_bar(session, customer):... another test
It reduces the boilerplate somewhat - in go, each test would be something likefunc test_foo() {session := make_session()
defer session.close()customer := make_customer(session)... rest of test}which is actually not unreasonable IMO.pytest also lets you have objects with longer lifetimes (scopes), so that multiple tests within the same package or session can share the same object.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9dfa197a-fa6b-4db1-b231-1de872b0b8e4n%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/gg7cstoAzbk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFQ0okmkZwqB%3D_GaCQH8Q9ECCiE7coM3Fb-8cL3pz32jg%40mail.gmail.com.