Giter Club home page Giter Club logo

testpackage's Introduction

testpackage
Build Status codecov codebeat badge Maintainability Go Report Card GitHub GoDoc

testpackage is a golang linter that makes you use a separate _test package.

Motivation

According to blackbox testing approach, you should not use unexported functions and methods from source code in tests.

Go allows to place tests in a separate package with suffix _test. For example, tests for store package can be in the same package or in the package store_test. In the second case, you have to import the source code into tests so only exported things are available.

The linter reports if a test is in a package without suffix _test. If you really need to test unexported function, then put the test into file XXX_internal_test.go. The linter skips such files by default. It also skips the file export_test.go by default (see the last article below).

More detailed articles on this topic:

Usage

The best way is to use golangci-lint. It includes testpackage linter started from v1.25.0 and higher.

Install

See install section of readme.

Configuration

testpackage is disabled by default. To enable it, add the following to your .golangci.yml:

linters:
  enable:
    testpackage

You can also change regexp that is used to ignore files by the linter. Here is the default value.

linters-settings:
  testpackage:
    skip-regexp: (export|internal)_test\.go

Run

golangci-lint run

Usage as standalone linter

Install

go get -u github.com/maratori/testpackage

Run

testpackage ./...

or

testpackage -skip-regexp="^$" ./...

Command line arguments

testpackage -help
testpackage: linter that makes you use a separate _test package

Usage: testpackage [-flag] [package]

Flags:  -V      print version and exit
  -skip-regexp string
        regexp pattern to skip file by name. To not skip files use -skip-regexp="^$" (default "(export|internal)_test\\.go")
  -json
        emit JSON output
  -c int
        display offending line with this many lines of context (default -1)
  -cpuprofile string
        write CPU profile to this file
  -memprofile string
        write memory profile to this file

Changelog

[v1.0.1] - 2020-04-22

Changed

  • No changes in linter behavior
  • Use latest go version on travis-ci
  • Update Makefile
  • Update golangci-lint

[v1.0.0] - 2019-11-10

Added

  • Go Analyzer to check the name of test package
  • main.go to run the analyzer
  • MIT license

testpackage's People

Contributors

maratori avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.