Skip to content

Commit 1b1da12

Browse files
authored
Merge pull request #16 from maratori/readme
Add motivation and instructions to readme
2 parents abeefe1 + 1fb13af commit 1b1da12

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ linters:
106106
issues:
107107
max-issues-per-linter: 0
108108
max-same-issues: 0
109+
exclude-rules:
110+
- path: doc.go
111+
linters: lll

README.md

+97-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,99 @@
11
# testpackage <br> [![Build Status](https://travis-ci.com/maratori/testpackage.svg?branch=master)](https://travis-ci.com/maratori/testpackage) [![codecov](https://codecov.io/gh/maratori/testpackage/branch/master/graph/badge.svg)](https://codecov.io/gh/maratori/testpackage) [![codebeat badge](https://codebeat.co/badges/9c74d700-ebf8-4b76-8405-1950874576c4)](https://codebeat.co/projects/g.yxqyang.asia-maratori-testpackage-master) [![Maintainability](https://api.codeclimate.com/v1/badges/bf753d7560c8e4aa5cf0/maintainability)](https://codeclimate.com/github/maratori/testpackage/maintainability) [![Go Report Card](https://goreportcard.com/badge/github.com/maratori/testpackage)](https://goreportcard.com/report/github.com/maratori/testpackage) [![GitHub](https://img.shields.io/github/license/maratori/testpackage.svg)](LICENSE) [![GoDoc](https://godoc.org/github.com/maratori/testpackage?status.svg)](http://godoc.org/github.com/maratori/testpackage)
22

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)

doc.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
testpackage is golang linter that makes you use a separate `_test` package.
3+
4+
Motivation
5+
6+
According to blackbox testing approach, you should not use unexported functions and methods from source code in tests.
7+
8+
Go allows to place tests in a separate package with suffix `_test`.
9+
For example, tests for `store` package can be in the same package or in the package `store_test`.
10+
In the second case, you have to import the source code into tests so only exported things are available.
11+
12+
The linter reports if a test is in a package without suffix `_test`.
13+
If you really need to test unexported function, then put the test into file `XXX_internal_test.go`.
14+
The linter skips such files by default.
15+
It also skips the file `export_test.go` by default (see the last article below).
16+
17+
More detailed articles on this topic:
18+
* Next level Go testing by Tit Petric https://scene-si.org/2019/04/15/next-level-go-testing#public-vs-private-tests-apis
19+
* 5 simple tips and tricks for writing unit tests in #golang by Mat Ryer https://medium.com/@matryer/5-simple-tips-and-tricks-for-writing-unit-tests-in-golang-619653f90742
20+
* 5 advanced testing techniques in Go by Alan Braithwaite https://segment.com/blog/5-advanced-testing-techniques-in-go/#use-a-separate-test-package
21+
* Golang Trick: Export unexport method for test by lysu https://medium.com/@robiplus/golang-trick-export-for-test-aa16cbd7b8cd
22+
*/
23+
package main

0 commit comments

Comments
 (0)