Agreed. I've always found the "just add an argument with the right name"
fixture feature too magical and implicit (read: confusing). I much prefer
Go's explicitness here, even if slightly more verbose.

IMO even in Python that kind of magic is usually frowned upon.

-Ben

On Thu, Nov 4, 2021 at 9:26 PM 'Axel Wagner' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> On Thu, Nov 4, 2021 at 8:41 AM Brian Candler <b.cand...@pobox.com> wrote:
>
>> 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_session
>>     db_session.close()
>>
>> @pytest.fixture(scope='function')
>> def customer(session):
>>     c = Customer(name="Fred")
>>     session.add(c)
>>     return c
>>
>> # The actual tests
>> def test_foo(session, customer):
>>     assert session is not None
>>     assert customer is not None
>>
>> def test_bar(session, customer):
>>     ... another test
>>
>
> Compare that to Go:
>
> func makeSession(t *testing.T) *Session {
>     s = make_session()
>     t.Cleanup(s.Close)
>     return s
> }
>
> func makeCustomer(t *testing.T, s *Session) Customer {
>     c = Customer{Name: "Fred"}
>     s.Add(c)
>     return c
> }
>
> // The actual tests
> func TestFoo(t *testing.T) {
>     s := makeSession(t)
>     c := makeCustomer(t, s)
>     // test code
> }
>
> ISTM the primary difference here is, that instead of adding `s *Session`
> to `TestFoo`, you add a `s := makeSession(t)` statement. I just don't see
> that as saving any significant amount of boilerplate. And if the complaint
> is that you then have to actually pass the created session to
> `makeCustomer`, you can always have a `makePopulatedSession` helper, if you
> have to do that too often.
>
> To be clear, I can see *some* benefit. Just not enough to offset the cost
> of using an obscure tool and thus making it much harder for other Go
> programmers to work on that project.
>
> YMMV, of course, which is why I left it at "I don't know any such tool".
>
>
>> It reduces the boilerplate somewhat - in go, each test would be something
>> like
>>
>> func 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+unsubscr...@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
>> <https://groups.google.com/d/msgid/golang-nuts/9dfa197a-fa6b-4db1-b231-1de872b0b8e4n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFQ0okmkZwqB%3D_GaCQH8Q9ECCiE7coM3Fb-8cL3pz32jg%40mail.gmail.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/CAL9jXCGLBE7NNqCW6ufeHWbq1Cy_GV8O1LzE-j%3DYEf5r%2BN6oFg%40mail.gmail.com.

Reply via email to