apitestengine

package module
v0.0.0-...-c1f4ae2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: GPL-3.0 Imports: 6 Imported by: 0

README

ApiTestEngine

It is wrapper to make http calles to given APIs using http GET, POST, PUT, DELETE methods against the given httptest.Server

Usage

  1. First create a httptest.Server, pass it to create new APITest engine.
// import package
import apitest "github.com/samtech09/apitestengine"
...
...
var chiTest *apitest.APITest


//create router/mux
r := chi.NewRouter()
r.Get("/", Index)
...


// create httptest server
s := httptest.NewServer(r)

// create and initialize test engine
chiTest = apitest.NewAPITest(s)
  1. Create test-endpoints and make testcases
// single case
func TestChiIndex(t *testing.T) {
	ret := chiTest.DoTest(apitest.NewTestCase("TestChiIndex", "GET", "/", "[string to match with response]", apitest.MatchTypeXXX, "", nil))
	if ret.Err != nil {
		t.Error(ret.Err)
	}
}

//multiple cases
func TestChiParam(t *testing.T) {
	tcs := []apitest.TestCase{}
	tcs = append(tcs, apitest.NewTestCase("TestChiParam", "GET", "/param/1", "key = 1, apitest.MatchExact, "", nil))
	tcs = append(tcs, apitest.NewTestCase("TestChiParam2", "GET", "/param/123", "key = 123", apitest.MatchContains, "", nil))

	tresult := chiTest.DoTests(tcs)
	for _, ret := range tresult {
		if ret.Err != nil {
			fmt.Println(ret.ID)
			t.Error(ret.Err)
		} else {
			fmt.Println("PASS: ", ret.ID)
		}
	}
}

View Examples for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APITest

type APITest struct {
	Ts *httptest.Server
}

APITest struct for out test framework

func NewAPITest

func NewAPITest(ts *httptest.Server) *APITest

NewAPITest creates and return new ApiTest initialized with given httptest.Server

func (*APITest) DoTest

func (t *APITest) DoTest(cs TestCase) TestCaseResult

DoTest run a single given test case

func (*APITest) DoTests

func (t *APITest) DoTests(cases []TestCase) []TestCaseResult

DoTests run all given test cases

type MatchType

type MatchType int
const (
	MatchExact MatchType = iota
	MatchStartsWith
	MatchEndsWith
	MatchContains
	MatchNotContains
)

type TestCase

type TestCase struct {
	ID                 string
	Method             string
	URL                string
	Expected           string
	Match              MatchType
	PostPutContentType string
	PostPutPayload     io.Reader
}

TestCase allow to set URL calling parameters

func NewTestCase

func NewTestCase(id, method, url, expected string, match MatchType, postPutContentType string, postPutPayload io.Reader) TestCase

NewTestCase creates and return new-test-case with given parameters

type TestCaseResult

type TestCaseResult struct {
	ID  string
	Err error
}

TestCaseResult containd error (if any) of given test case

Directories

Path Synopsis
example
chi
gin

Jump to

Keyboard shortcuts

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