gotestlist

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 11 Imported by: 0

README

PkgGoDev Test workflow Go Report Codecov

About

List tests in the given Go packages.

Installation

$ go install github.com/crazy-max/gotestlist/cmd/gotestlist@latest

Usage

$ gotestlist .
gotestlist     TestTests     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
$ gotestlist ./...
gotestlist     TestTests     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
main           TestDirs      /home/crazy/src/github.com/crazy-max/gotestlist/cmd/gotestlist/gotestlist_test.go
$ gotestlist github.com/crazy-max/gotestlist
gotestlist     TestTests     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
$ gotestlist github.com/crazy-max/gotestlist/...
gotestlist     TestTests     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
main           TestDirs      /home/crazy/src/github.com/crazy-max/gotestlist/cmd/gotestlist/gotestlist_test.go
$ gotestlist github.com/crazy-max/gotestlist github.com/crazy-max/gotestlist/cmd/gotestlist
gotestlist     TestTests     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
main           TestDirs      /home/crazy/src/github.com/crazy-max/gotestlist/cmd/gotestlist/gotestlist_test.go
$ gotestlist -f json ./... | jq
[
  {
    "name": "TestTests",
    "benchmark": false,
    "fuzz": false,
    "suite": "",
    "file": "/home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go",
    "pkg": "gotestlist"
  },
  {
    "name": "TestDirs",
    "benchmark": false,
    "fuzz": false,
    "suite": "",
    "file": "/home/crazy/src/github.com/crazy-max/gotestlist/cmd/gotestlist/gotestlist_test.go",
    "pkg": "main"
  }
]
$ gotestlist -f "Pkg: {{.Pkg}} | TestName: {{.Name}} | File: {{.File}}" ./...
Pkg:     gotestlist     |     TestName:     TestTests     |     File:     /home/crazy/src/github.com/crazy-max/gotestlist/gotestlist_test.go
Pkg:     main           |     TestName:     TestDirs      |     File:     /home/crazy/src/github.com/crazy-max/gotestlist/cmd/gotestlist/gotestlist_test.go
$ gotestlist -d 1 ./...
["TestDirs|TestTests"]
$ gotestlist -d 2 ./...
["TestDirs","TestTests"]

Distribute tests with GitHub Actions

-d flag dynamically distributes the tests based on the given matrix size and number of tests found. This JSON output can then be used as matrix input in a GitHub Action workflow:

name: test

on:
  push:

env:
  GO_VERSION: 1.18

jobs:
  test-prepare:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.tests.outputs.matrix }}
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      -
        name: Install gotestlist
        run:
          go install github.com/crazy-max/gotestlist/cmd/gotestlist@latest
      -
        name: Create matrix
        id: tests
        run: |
          matrix="$(gotestlist -d 4 ./...)"
          echo "::set-output name=matrix::$matrix"
  
  test:
    runs-on: ubuntu-latest
    needs:
      - test-prepare
    strategy:
      fail-fast: false
      test: ${{ fromJson(needs.test-prepare.outputs.matrix) }}
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      -
        name: Test
        run: |
          go test -run=(${{ matrix.test }})/ -coverprofile=coverage.txt -covermode=atomic ./...
      -
        name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.txt

This is useful if you have a lot of tests and you want to distribute them to reduce build time.

Contributing

Want to contribute? Awesome! The most basic way to show your support is to star the project, or to raise issues. You can also support this project by becoming a sponsor on GitHub or by making a Paypal donation to ensure this journey continues indefinitely!

Thanks again for your support, it is much appreciated! 🙏

License

MIT. See LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Test

type Test struct {
	Name      string `json:"name"`
	Benchmark bool   `json:"benchmark"`
	Fuzz      bool   `json:"fuzz"`
	Suite     string `json:"suite"`
	Pkg       string `json:"pkg"`
	File      string `json:"file"`
}

Test describes a single test found in the *_test.go file

func (*Test) String

func (t *Test) String() string

String returns a string representation of the Test in the form of 'package.Test filename'

type TestSlice

type TestSlice []Test

TestSlice attaches the methods of sort.Interface to []Test. Sorting in increasing order comparing package+testname.

func Tests

func Tests(dir string) (tests TestSlice, err error)

Tests function searches for test function declarations in the given directory.

func (TestSlice) Len

func (s TestSlice) Len() int

func (TestSlice) Less

func (s TestSlice) Less(i, j int) bool

func (TestSlice) Sort

func (s TestSlice) Sort()

Sort is a convenience method.

func (TestSlice) Swap

func (s TestSlice) Swap(i, j int)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL