|
1 | 1 | # testpackage <br> [](https://travis-ci.com/maratori/testpackage) [](https://codecov.io/gh/maratori/testpackage) [](https://codebeat.co/projects/g.yxqyang.asia-maratori-testpackage-master) [](https://codeclimate.com/github/maratori/testpackage/maintainability) [](https://goreportcard.com/report/github.com/maratori/testpackage) [](LICENSE) [](http://godoc.org/github.com/maratori/testpackage)
|
2 | 2 |
|
3 |
| -Golang linter that makes you use a separate `_test` package. |
| 3 | +**testpackage** is a golang linter that makes you use a separate `_test` package. |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +According to blackbox testing approach, you should not use unexported functions and methods from source code in tests. |
| 8 | + |
| 9 | +Go allows to place tests in a separate package with suffix `_test`. |
| 10 | +For example, tests for `store` package can be in the same package or in the package `store_test`. |
| 11 | +In the second case, you have to import the source code into tests so only exported things are available. |
| 12 | + |
| 13 | +The linter reports if a test is in a package without suffix `_test`. |
| 14 | +If you really need to test unexported function, then put the test into file `XXX_internal_test.go`. |
| 15 | +The linter skips such files by default. |
| 16 | +It also skips the file `export_test.go` by default (see the last article below). |
| 17 | + |
| 18 | +More detailed articles on this topic: |
| 19 | + * [Next level Go testing](https://scene-si.org/2019/04/15/next-level-go-testing#public-vs-private-tests-apis) by Tit Petric |
| 20 | + * [5 simple tips and tricks for writing unit tests in #golang](https://medium.com/@matryer/5-simple-tips-and-tricks-for-writing-unit-tests-in-golang-619653f90742) by Mat Ryer |
| 21 | + * [5 advanced testing techniques in Go](https://segment.com/blog/5-advanced-testing-techniques-in-go/#use-a-separate-test-package) by Alan Braithwaite |
| 22 | + * [Golang Trick: Export unexport method for test](https://medium.com/@robiplus/golang-trick-export-for-test-aa16cbd7b8cd) by lysu |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +The best way is to use [golangci-lint](https://github.com/golangci/golangci-lint). |
| 27 | +It includes **testpackage** linter started from v1.22.0 and higher. |
| 28 | + |
| 29 | +### Install |
| 30 | +See [install section](https://github.com/golangci/golangci-lint#install) of readme. |
| 31 | + |
| 32 | +### Configuration |
| 33 | +**testpackage** is disabled by default. To enable it, add the following to your `.golangci.yml`: |
| 34 | +```yaml |
| 35 | +linters: |
| 36 | + enable: |
| 37 | + testpackage |
| 38 | +``` |
| 39 | +
|
| 40 | +You can also change regexp that is used to ignore files by the linter. Here is the default value. |
| 41 | +```yaml |
| 42 | +linters-settings: |
| 43 | + testpackage: |
| 44 | + skip-regexp: (export|internal)_test\.go |
| 45 | +``` |
| 46 | +
|
| 47 | +### Run |
| 48 | +```shell script |
| 49 | +golangci-lint run |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +## Usage as standalone linter |
| 54 | + |
| 55 | +### Install |
| 56 | +```shell script |
| 57 | +go get -u github.com/maratori/testpackage |
| 58 | +``` |
| 59 | + |
| 60 | +### Run |
| 61 | +```shell script |
| 62 | +testpackage ./... |
| 63 | +``` |
| 64 | +or |
| 65 | +```shell script |
| 66 | +testpackage -skip-regexp="^$" ./... |
| 67 | +``` |
| 68 | + |
| 69 | +### Command line arguments |
| 70 | +```shell script |
| 71 | +testpackage -help |
| 72 | +``` |
| 73 | +``` |
| 74 | +testpackage: linter that makes you use a separate _test package |
| 75 | +
|
| 76 | +Usage: testpackage [-flag] [package] |
| 77 | +
|
| 78 | +Flags: -V print version and exit |
| 79 | + -skip-regexp string |
| 80 | + regexp pattern to skip file by name. To not skip files use -skip-regexp="^$" (default "(export|internal)_test\\.go") |
| 81 | + -json |
| 82 | + emit JSON output |
| 83 | + -c int |
| 84 | + display offending line with this many lines of context (default -1) |
| 85 | + -cpuprofile string |
| 86 | + write CPU profile to this file |
| 87 | + -memprofile string |
| 88 | + write memory profile to this file |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +## Changelog |
| 93 | + |
| 94 | +### [v1.0.0] - 2019-11-10 |
| 95 | + |
| 96 | +#### Added |
| 97 | +* Go Analyzer to check the name of test package |
| 98 | +* main.go to run the analyzer |
| 99 | +* MIT [license](LICENSE) |
0 commit comments