github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/docs/Test.md (about)

     1  Summary
     2  -------
     3  `./godelw test` runs the Go tests in the project.
     4  
     5  Tutorial start state
     6  --------------------
     7  
     8  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
     9  * Project contains `godel` and `godelw`
    10  * Project contains `main.go`
    11  * Project contains `.gitignore` that ignores IDEA files
    12  * Project contains `echo/echo.go` and `echo/echoer.go`
    13  
    14  ([Link](https://github.com/nmiyake/echgo/tree/0a649925e317b7896e537ef23a4885062a3ec9fb))
    15  
    16  Run tests
    17  ---------
    18  
    19  We will now add some tests to our program. Run the following to add tests for the `echo` package:
    20  
    21  ```
    22  ➜ echo 'package echo_test
    23  
    24  import (
    25  	"testing"
    26  
    27  	"github.com/nmiyake/echgo/echo"
    28  )
    29  
    30  func TestEcho(t *testing.T) {
    31  	echoer := echo.NewEchoer()
    32  	for i, tc := range []struct {
    33  		in   string
    34  		want string
    35  	}{
    36  		{"foo", "foo"},
    37  		{"foo bar", "foo bar"},
    38  	} {
    39  		if got := echoer.Echo(tc.in); got != tc.want {
    40  			t.Errorf("case %d failed: want %q, got %q", i, tc.want, got)
    41  		}
    42  	}
    43  }' > echo/echo_test.go
    44  ```
    45  
    46  Run `./godelw test` to run all of the Go tests in the project:
    47  
    48  ```
    49  ➜ ./godelw test
    50  ok  	github.com/nmiyake/echgo     	0.090s [no tests to run]
    51  ok  	github.com/nmiyake/echgo/echo	0.132s
    52  ```
    53  
    54  Commit the test to the repository:
    55  
    56  ```
    57  ➜ git add echo
    58  ➜ git commit -m "Add tests for echo package"
    59  [master 404c745] Add tests for echo package
    60   1 file changed, 22 insertions(+)
    61   create mode 100644 echo/echo_test.go
    62  ➜ git status
    63  On branch master
    64  nothing to commit, working directory clean
    65  ```
    66  
    67  Tutorial end state
    68  ------------------
    69  
    70  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
    71  * Project contains `godel` and `godelw`
    72  * Project contains `main.go`
    73  * Project contains `.gitignore` that ignores IDEA files
    74  * Project contains `echo/echo.go`, `echo/echo_test.go` and `echo/echoer.go`
    75  
    76  ([Link](https://github.com/nmiyake/echgo/tree/404c745e6bc0f70f4d4b58b60502e5b9620a00a7))
    77  
    78  Tutorial next step
    79  ------------------
    80  
    81  [Build](https://github.com/palantir/godel/wiki/Build)
    82  
    83  More
    84  ----
    85  
    86  ### Differences between `./godelw test` and `go test ./...`
    87  
    88  `./godelw test` has the following advantages over running `go test ./...`:
    89  
    90  * Aligns the output so that all of the test times line up
    91  * Generates placeholder files in packages that do not contain tests (important for coverage purposes)
    92  * Only runs tests for files that are part of the project (does not run tests in vendor directories)
    93    * This has been fixed in the Go tool itself as of Go 1.9
    94  
    95  To demonstrate this, check out the project `github.com/palantir/checks`:
    96  
    97  ```
    98  ➜ mkdir -p $GOPATH/src/github.com/palantir && cd $_
    99  ➜ git clone https://github.com/palantir/checks.git
   100  Cloning into 'checks'...
   101  remote: Counting objects: 2800, done.
   102  remote: Compressing objects: 100% (9/9), done.
   103  remote: Total 2800 (delta 1), reused 3 (delta 0), pack-reused 2791
   104  Receiving objects: 100% (2800/2800), 6.45 MiB | 4.21 MiB/s, done.
   105  Resolving deltas: 100% (660/660), done.
   106  Checking connectivity... done.
   107  ➜ cd checks
   108  ```
   109  
   110  In Go 1.8 and earlier, running `go test ./...` fails immediately because there is code in the vendor directory that
   111  cannot be built:
   112  
   113  ```
   114  ➜ go test ./...
   115  vendor/github.com/palantir/godel/apps/distgo/cmd/publish/github_publish.go:27:2: cannot find package "github.com/google/go-github/github" in any of:
   116  	/Volumes/git/go2/src/github.com/palantir/checks/vendor/github.com/google/go-github/github (vendor tree)
   117  ...
   118  ```
   119  
   120  Go 1.9 fixes this issue by having `./...` no longer match paths in vendor directories. In Go 1.8 and earlier, the
   121  equivalent can be achieved by running `go test $(go list ./... | grep -v /vendor/)`. Doing so produces the following:
   122  
   123  ```
   124  ➜ go test $(go list ./... | grep -v /vendor/)
   125  ok  	github.com/palantir/checks/compiles	4.867s
   126  ok  	github.com/palantir/checks/extimport	1.296s
   127  ?   	github.com/palantir/checks/gocd	[no test files]
   128  ?   	github.com/palantir/checks/gocd/cmd	[no test files]
   129  ?   	github.com/palantir/checks/gocd/cmd/gocd	[no test files]
   130  ok  	github.com/palantir/checks/gocd/config	1.416s
   131  ok  	github.com/palantir/checks/gocd/gocd	0.449s
   132  ?   	github.com/palantir/checks/gogenerate	[no test files]
   133  ?   	github.com/palantir/checks/gogenerate/cmd	[no test files]
   134  ?   	github.com/palantir/checks/gogenerate/cmd/gogenerate	[no test files]
   135  ok  	github.com/palantir/checks/gogenerate/config	1.416s
   136  ok  	github.com/palantir/checks/gogenerate/gogenerate	8.249s
   137  ?   	github.com/palantir/checks/golicense	[no test files]
   138  ?   	github.com/palantir/checks/golicense/cmd	[no test files]
   139  ?   	github.com/palantir/checks/golicense/cmd/golicense	[no test files]
   140  ok  	github.com/palantir/checks/golicense/config	1.183s
   141  ok  	github.com/palantir/checks/golicense/golicense	0.984s
   142  ok  	github.com/palantir/checks/importalias	2.157s
   143  ?   	github.com/palantir/checks/nobadfuncs	[no test files]
   144  ok  	github.com/palantir/checks/nobadfuncs/integration_test	13.334s
   145  ok  	github.com/palantir/checks/nobadfuncs/nobadfuncs	13.895s
   146  ok  	github.com/palantir/checks/novendor	2.132s
   147  ?   	github.com/palantir/checks/outparamcheck	[no test files]
   148  ok  	github.com/palantir/checks/outparamcheck/exprs	1.905s
   149  ok  	github.com/palantir/checks/outparamcheck/outparamcheck	1.729s
   150  ?   	github.com/palantir/checks/ptimports	[no test files]
   151  ?   	github.com/palantir/checks/ptimports/ptimports	[no test files]
   152  ```
   153  
   154  Compare this to running `./godelw test`:
   155  
   156  ```
   157  ➜ ./godelw test
   158  ok  	github.com/palantir/checks/compiles                   	4.242s
   159  ok  	github.com/palantir/checks/extimport                  	0.928s
   160  ok  	github.com/palantir/checks/gocd                       	0.444s [no tests to run]
   161  ok  	github.com/palantir/checks/gocd/cmd                   	1.693s [no tests to run]
   162  ok  	github.com/palantir/checks/gocd/cmd/gocd              	1.600s [no tests to run]
   163  ok  	github.com/palantir/checks/gocd/config                	1.547s
   164  ok  	github.com/palantir/checks/gocd/gocd                  	0.962s
   165  ok  	github.com/palantir/checks/gogenerate                 	0.626s [no tests to run]
   166  ok  	github.com/palantir/checks/gogenerate/cmd             	0.759s [no tests to run]
   167  ok  	github.com/palantir/checks/gogenerate/cmd/gogenerate  	0.948s [no tests to run]
   168  ok  	github.com/palantir/checks/gogenerate/config          	1.207s
   169  ok  	github.com/palantir/checks/gogenerate/gogenerate      	5.901s
   170  ok  	github.com/palantir/checks/golicense                  	0.929s [no tests to run]
   171  ok  	github.com/palantir/checks/golicense/cmd              	0.818s [no tests to run]
   172  ok  	github.com/palantir/checks/golicense/cmd/golicense    	0.986s [no tests to run]
   173  ok  	github.com/palantir/checks/golicense/config           	0.857s
   174  ok  	github.com/palantir/checks/golicense/golicense        	0.929s
   175  ok  	github.com/palantir/checks/importalias                	1.123s
   176  ok  	github.com/palantir/checks/nobadfuncs                 	0.360s [no tests to run]
   177  ok  	github.com/palantir/checks/nobadfuncs/nobadfuncs      	9.438s
   178  ok  	github.com/palantir/checks/novendor                   	1.059s
   179  ok  	github.com/palantir/checks/outparamcheck              	0.564s [no tests to run]
   180  ok  	github.com/palantir/checks/outparamcheck/exprs        	0.775s
   181  ok  	github.com/palantir/checks/outparamcheck/outparamcheck	0.807s
   182  ok  	github.com/palantir/checks/ptimports                  	0.168s [no tests to run]
   183  ok  	github.com/palantir/checks/ptimports/ptimports        	0.847s [no tests to run]
   184  ```
   185  
   186  This output is much easier to read and has the advantage of ignoring all excluded directories.
   187  
   188  Restore the working directory to be the original directory:
   189  
   190  ```
   191  ➜ cd $GOPATH/src/github.com/nmiyake/echgo
   192  ```
   193  
   194  ### Generate coverage reports
   195  
   196  A combined coverage report for the project can be generated by running the command
   197  `./godelw test cover --coverage-output=<file>`:
   198  
   199  ```
   200  ➜ ./godelw test cover --coverage-output=cover.out
   201  ok  	github.com/nmiyake/echgo     	0.108s	coverage: 0.0% of statements [no tests to run]
   202  ok  	github.com/nmiyake/echgo/echo	0.161s	coverage: 100.0% of statements
   203  ➜ cat cover.out
   204  mode: count
   205  github.com/nmiyake/echgo/main.go:11.13,14.2 2 0
   206  github.com/nmiyake/echgo/echo/echo.go:3.25,5.2 1 1
   207  github.com/nmiyake/echgo/echo/echo.go:9.47,11.2 1 2
   208  ```
   209  
   210  This command runs the Go tests all of the packages in the project in coverage mode using `-covermode=count` and
   211  generates a single combined coverage file for all of the packages in the project.
   212  
   213  In contrast, running `go test --cover ./...` produces the following:
   214  
   215  ```
   216  ➜ go test --cover ./...
   217  ?   	github.com/nmiyake/echgo	[no test files]
   218  ok  	github.com/nmiyake/echgo/echo	0.090s	coverage: 100.0% of statements
   219  ```
   220  
   221  Go cover does not count packages that do not contain tests towards the coverage count by default, and also does not
   222  provide a single command that allows the use of profile flags with multiple packages.
   223  
   224  Remove the output by running the following:
   225  
   226  ```
   227  ➜ rm cover.out
   228  ```
   229  
   230  ### Generate JUnit reports
   231  
   232  The `./godelw test --junit-output=<file>` command can be used to generate a JUnit-style output XML file that summarizes
   233  the results of running the tests:
   234  
   235  ```
   236  ➜ ./godelw test --junit-output=output.xml
   237  testing: warning: no tests to run
   238  PASS
   239  ok  	github.com/nmiyake/echgo     	0.121s [no tests to run]
   240  === RUN   TestEcho
   241  --- PASS: TestEcho (0.00s)
   242  PASS
   243  ok  	github.com/nmiyake/echgo/echo	0.112s
   244  ➜ cat output.xml
   245  <?xml version="1.0" encoding="UTF-8"?>
   246  <testsuites>
   247  	<testsuite tests="1" failures="0" time="0.112" name="github.com/nmiyake/echgo/echo">
   248  		<properties>
   249  			<property name="go.version" value="go1.9"></property>
   250  		</properties>
   251  		<testcase classname="echo" name="TestEcho" time="0.000"></testcase>
   252  	</testsuite>
   253  </testsuites>
   254  ```
   255  
   256  Remove the output by running the following:
   257  
   258  ```
   259  ➜ rm output.xml
   260  ```