cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.97k stars 346 forks source link

Use subtests once Go 1.7 is released #23

Closed cweill closed 8 years ago

cweill commented 8 years ago

Subtests are introduced in Go 1.7. They'll be very useful for table driven tests.

See https://github.com/golang/go/issues/9268

iMaxopoly commented 8 years ago

+1 This is an amazing work you've done here. Thanks for spoon-feeding us lazy bums :)

itsjamie commented 8 years ago

@cweill Is this something you would accept a PR for? I just started using the package in VSCode and would write this up soon if it isn't already being worked on.

I assume it should conditionally use the old template for 1.6, and use the subtests template for >=1.7?

cweill commented 8 years ago

@itsjamie I would be open to PR if you are interested in taking this on. I know a lot of people who would really appreciate this feature, but I haven't had the time to work on this lately.

There would be three requirements:

  1. It should use the same templates to generate the subtests.
  2. To maintain backwards compatibility, we could conditionally check the go version, though I'm not sure how to do that exactly, and it may make the code more difficult to maintain. Alternatively, we can add a flag like --no_subtests, which allows users to disable the feature it if they don't like the format, or if they are using an older version of Go.
  3. Tests should be regenerated to use the subtests format. This is really easy to do by finding the tmp directory where the gotests package tests output generated test files, and simply copying them to the testdata/ dir.
itsjamie commented 8 years ago

I've started to work on this.

The approach I'm taking is creating a new template for >= Go 1.7.

Then, maintain the backwards compatibility I'm using build tags, so for <Go 1.7, the template functions target the existing template, and for Go 1.7+ they target the new template.

I moved the template into two separate files just to see all the differences. If you would rather have it just use conditions in the same template, that can also be done. I'll put the PR up when I get access to my personal computer today so you can review and let me know what I need to fix.

cweill commented 8 years ago

Sounds good! Looking forward to your PR.

cweill commented 8 years ago

@itsjamie thanks again for your help! I posted on r/golang, and it seems like a lot of Gophers are excited to have this feature.

https://www.reddit.com/r/golang/comments/5bo6tp/gotests_test_generation_tool_now_supports/

itsjamie commented 8 years ago

Neat. Thanks for letting me. Now I can work on getting the behaviour and flag supported in vscode :) In case you didn't know they use your binary in the official vscode-go extension.