Skip to content
A tool to generate Go test functions from the given source code like gofmt
Go Vim script Emacs Lisp Makefile Shell
Branch: master
Clone or download
Latest commit 956d6df Feb 9, 2016

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
editor Document Feb 9, 2016
test-fixtures Add fixtures Jan 27, 2016
tests Add simple test Jan 18, 2016
.gitignore Update ignore Jan 18, 2016
CHANGELOG.md Initial commit Jan 17, 2016
LICENSE Add LICENSE Jan 24, 2016
Makefile govet Feb 1, 2016
README.md Document Feb 9, 2016
ast.go Refactoring && enable directory walk Jan 24, 2016
ast_test.go Generate test by gotests Jan 27, 2016
cli.go Documentation Feb 2, 2016
cli_test.go Generate test by gotests Jan 27, 2016
doc.go Documentation Feb 2, 2016
doc_test.go Generate test by gotests Jan 27, 2016
go.go Add reverse option Jan 27, 2016
go_test.go Generate test by gotests Jan 27, 2016
main.go Refactoring && enable directory walk Jan 24, 2016
main_test.go Generate test by gotests Jan 27, 2016
parse.go Enable to parse/add method Jan 27, 2016
parse_test.go Generate test by gotests Jan 27, 2016
version.go Change tool name Jan 25, 2016
version_test.go Generate test by gotests Jan 27, 2016

README.md

gotests

MIT License Go Documentation

gotests generates Go test functions from the given source code.

Given A.go file, it analyzes test functions in A_test.go and adds test functions which are not defined in that file. For example, if a function DoSomething() is defined in A.go but TestDoSomething() is not in A_test.go, then it adds that function to A_test.go. By default, it only checks the exported functions (its name starts with upper case). Given a directory, it operates on all *.go files in that directory. By default, gotests prints the updated test sources to standard output.

Editor integration

gotests works well with your favorite editor capable of running an external tool like gofmt does. The following demo shows using gotests from Emacs. The left display shows the source codes (A.go) and the right shows test source codes (A_test.go). It generates and adds test functions on the right codes,

demo

gotests.el used by this demo is available in editor/emacs directory (I'm not good at emacs package development. So this plugin should not well written. If you are good at emacs plugin please send PR 🙇 ). Now we have the following editor supports,

A plugin PR for the other editor is welcome.

Install

To install, use go get:

$ go get -u github.com/tcnksm/gotests

Usage

The usage:

gotests [options] PATH ...

Available options:

-diff, -d      Display diffs instead of rewriting files.

-write, -w     Write result to target file instead of stdout.
               For example, if source file name is 'A.go', target
               file would be 'A_test.go'.

-list, -l      List test files to be updated/generated.

-i             Include unexport function/method for generating target.

-reverse, -r   (experimental) Allow to input test file instead of source file.
               By default, gotests expects source file PATH provided.
               With this flag, the test file can be given. 
               For example, you can provide 'A_test.go' instead of 'A.go'.
               This flag is useful for editor integration.

Rule

The followings are the current basic rule of how to generate test functions,

  • Test functions for A.go are added to A_test.go
  • Generating test function name of function is Test{{ title .Name }}
  • Generating test function name of method is Test{{ title .ReceiverTypeName }}_{{ title .Name }}

(In future, this rule will be configurable from command line option)

Contribution

  1. Fork (https://github.com/tcnksm/gotests/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create a new Pull Request

Author

Taichi Nakashima

You can’t perform that action at this time.