gotests

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

gotests License godoc Build Status Coverage Status codebeat badge Go Report Card

gotests makes writing Go tests easy. It's a Golang commandline tool that generates table driven tests based on its target source files' function and method signatures. Any new dependencies in the test files are automatically imported.

Demo

The following shows gotests in action using the official Sublime Text 3 plugin. Plugins also exist for Emacs, also Emacs, Vim, Atom Editor, Visual Studio Code, and IntelliJ Goland.

demo

Installation

Minimum Go version: Go 1.6

Use go get to install and update:

$ go get -u github.com/2997215859/gotests/...

Usage

From the commandline, gotests can generate Go tests for specific source files or an entire directory. By default, it prints its output to stdout.

$ gotests [options] PATH ...

Available options:

  -all                  generate tests for all functions and methods

  -excl                 regexp. generate tests for functions and methods that don't
                         match. Takes precedence over -only, -exported, and -all

  -exported             generate tests for exported functions and methods. Takes
                         precedence over -only and -all

  -i                    print test inputs in error messages

  -only                 regexp. generate tests for functions and methods that match only.
                         Takes precedence over -all

  -nosubtests           disable subtest generation when >= Go 1.7

  -parallel             enable parallel subtest generation when >= Go 1.7.

  -w                    write output to (test) files instead of stdout

  -template_dir         Path to a directory containing custom test code templates. Takes
                         precedence over -template. This can also be set via environment
                         variable GOTESTS_TEMPLATE_DIR

  -template             Specify custom test code templates, e.g. testify. This can also
                         be set via environment variable GOTESTS_TEMPLATE

  -template_params_file read external parameters to template by json with file

  -template_params      read external parameters to template by json with stdin

Contributions

Contributing guidelines are in CONTRIBUTING.md.

License

gotests is released under the Apache 2.0 License.

Documentation

Overview

Package gotests contains the core logic for generating table-driven tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GeneratedTest

type GeneratedTest struct {
	Path      string             // The test file's absolute path.
	Functions []*models.Function // The functions with new test methods.
	Output    []byte             // The contents of the test file.
}

A GeneratedTest contains information about a test file with generated tests.

func GenerateTests

func GenerateTests(srcPath string, opt *Options) ([]*GeneratedTest, error)

GenerateTests generates table-driven tests for the function and method signatures defined in the target source path file(s). The source path parameter can be either a Go source file or directory containing Go files.

type Options

type Options struct {
	Only           *regexp.Regexp         // Includes only functions that match.
	Exclude        *regexp.Regexp         // Excludes functions that match.
	Exported       bool                   // Include only exported methods
	PrintInputs    bool                   // Print function parameters in error messages
	Subtests       bool                   // Print tests using Go 1.7 subtests
	Parallel       bool                   // Print tests that runs the subtests in parallel.
	Named          bool                   // Create Map instead of slice
	Importer       func() types.Importer  // A custom importer.
	Template       string                 // Name of custom template set
	TemplateDir    string                 // Path to custom template set
	TemplateParams map[string]interface{} // Custom external parameters
	TemplateData   [][]byte               // Data slice for templates
}

Options provides custom filters and parameters for generating tests.

Directories

Path Synopsis
A commandline tool for generating table-driven Go tests.
A commandline tool for generating table-driven Go tests.
process
Package process is a thin wrapper around the gotests library.
Package process is a thin wrapper around the gotests library.
internal
goparser
Package goparse contains logic for parsing Go files.
Package goparse contains logic for parsing Go files.

Jump to

Keyboard shortcuts

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