Re: [go-nuts] Avoiding duplication in parallel tests

2018-01-18 Thread roger peppe
> - Is there a way to avoid the extra level of Run() nesting? It's a little 
> annoying when specifying a command to `go test -run`.

That's an interesting question.

Here's one possible solution: use reference counting to clean up the
resource after us. Since we
can rely on the fact that all parallel tests are started
synchronously, something like this, perhaps:
https://play.golang.org/p/6EphXfFn_pv

Note that the resource is passed to the tests, which I think is
usually what one wants.
It relies on the fact that all parallel t

- Is there any way to automatically populate the test name from the
function name? I could probably hack up something with the reflect
package based on the function name but that would also be pretty ugly,
I'm guessing.

Not in a nice way, no. For me, subtests like this that I have are
usually table-driven, so
I just put the name in the table.

  cheers,
rog.

On 18 January 2018 at 00:26, Kevin Burke  wrote:
> I have a bunch of tests I would like to run in parallel; I think this is a
> common situation for database backed tests where you might need to establish
> a connection or a transaction, run a bunch of queries, and then truncate the
> database. Right now I have something like this:
>
> func testA(t *testing.T) { t.Parallel(); ... }
>
> func testB(t *testing.T) { t.Parallel(); ... }
>
> func testC(t *testing.T) { t.Parallel(); ... }
>
> func testD(t *testing.T) { t.Parallel(); ... }
>
> func TestAll(t *testing.T) {
> setUp(t)
> defer tearDown(t)
> t.Run("parallel", func(t *testing.T) {
> t.Run("TestA", testA)
> t.Run("TestB", testB)
> t.Run("TestC", testC)
> t.Run("TestD", testD)
> })
> }
>
> However there's some duplication there.
>
> - Is there a way to avoid the extra level of Run() nesting? It's a little
> annoying when specifying a command to `go test -run`.
>
> - Is there any way to automatically populate the test name from the function
> name? I could probably hack up something with the reflect package based on
> the function name but that would also be pretty ugly, I'm guessing.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Avoiding duplication in parallel tests

2018-01-17 Thread Kevin Burke
I have a bunch of tests I would like to run in parallel; I think this is a 
common situation for database backed tests where you might need to 
establish a connection or a transaction, run a bunch of queries, and then 
truncate the database. Right now I have something like this:

func testA(t *testing.T) { t.Parallel(); ... }

func testB(t *testing.T) { t.Parallel(); ... }

func testC(t *testing.T) { t.Parallel(); ... }

func testD(t *testing.T) { t.Parallel(); ... }

func TestAll(t *testing.T) {
setUp(t)
defer tearDown(t)
t.Run("parallel", func(t *testing.T) {
t.Run("TestA", testA)
t.Run("TestB", testB)
t.Run("TestC", testC)
t.Run("TestD", testD)
})
}

However there's some duplication there. 

- Is there a way to avoid the extra level of Run() nesting? It's a little 
annoying when specifying a command to `go test -run`.

- Is there any way to automatically populate the test name from the 
function name? I could probably hack up something with the reflect package 
based on the function name but that would also be pretty ugly, I'm guessing.

-- 
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.
For more options, visit https://groups.google.com/d/optout.