gotest

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: MIT Imports: 10 Imported by: 0

README

Go (API) Test 2.0

A Simple and Minimalist API Testing Library for Go Language

Introduction

Go Test is a simple and minimalist testing library for Go language. It is designed to be simple and easy to use, with a focus on simplicity and minimalism.

Features

  • Simple and Minimalist API Testing Library: Streamlined for ease of use and quick setup.
  • Tabulated Results: Outputs test results in a clear, easy-to-read table format.
  • Response Time Calculation: Measures and reports the response time of each API request.
  • Response Time Evaluation: Categorizes and displays the response time category of each API request.
  • Response Status Evaluation: Determines and displays the success or failure status of API responses.

The table below outlines the response time categories used by Go Test to evaluate the performance of API requests:

Category Description Time Range
Best Optimal performance, ideal user experience, and minimal latency. 0 - 100 ms
Good Acceptable performance with minor delays, generally not noticeable to users. 100 - 300 ms
Acceptable Noticeable delays, but still within acceptable limits for most users. 300 - 1000 ms (1s)
Poor Significant delays, negatively impacting user experience. 1 - 2 seconds
Worst Unacceptable performance, leading to frustration and potential abandonment of the application. > 2 seconds
Invalid Errors or timeout conditions where the API fails to respond within a reasonable time frame. Timeout or Error

Getting Started

Installation

To use Go Test in your Go project, you need to install it using Go modules. You can add it to your project with the following command:

go get -u github.com/Tvative/Go-Test/v2

Documentation

For detailed documentation, check the package for this project

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Overview

Package gotest is a simple package for testing APIs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ContentTypeJson  = "application/json"                  // ContentTypeJson is for APIs with Json content.
	ContentTypeXml   = "application/xml"                   // ContentTypeXml is for APIs with Xml content.
	ContentTypeForm  = "application/x-www-form-urlencoded" // ContentTypeForm is for APIs with Form content.
	ContentTypeText  = "text/plain"                        // ContentTypeText is for APIs with Text content.
	ContentTypeHtml  = "text/html"                         // ContentTypeHtml is for APIs with Html content.
	ContentTypePdf   = "application/pdf"                   // ContentTypePdf is for APIs with Pdf content.
	ContentTypeZip   = "application/zip"                   // ContentTypeZip is for APIs with Zip content.
	ContentTypePng   = "image/png"                         // ContentTypePng is for APIs with Png content.
	ContentTypeJpg   = "image/jpeg"                        // ContentTypeJpg is for APIs with Jpg content.
	ContentTypeGif   = "image/gif"                         // ContentTypeGif is for APIs with Gif content.
	ContentTypeSvg   = "image/svg+xml"                     // ContentTypeSvg is for APIs with Svg content.
	ContentTypeBmp   = "image/bmp"                         // ContentTypeBmp is for APIs with Bmp content.
	ContentTypeTiff  = "image/tiff"                        // ContentTypeTiff is for APIs with Tiff content.
	ContentTypePpt   = "application/vnd.ms-powerpoint"     // ContentTypePpt is for APIs with Ppt content.
	ContentTypeDoc   = "application/msword"                // ContentTypeDoc is for APIs with Doc content.
	ContentTypeXls   = "application/vnd.ms-excel"          // ContentTypeXls is for APIs with Xls content.
	ContentTypeCsv   = "text/csv"                          // ContentTypeCsv is for APIs with Csv content.
	ContentTypeXml2  = "application/xml; charset=utf-8"    // ContentTypeXml2 is for APIs with Xml2 content.
	ContentTypeHtml2 = "text/html; charset=utf-8"          // ContentTypeHtml2 is for APIs with Html2 content.
)

Functions

func GeneratePathParam

func GeneratePathParam(getParam map[string]string) string

GeneratePathParam function takes a map of string key-value pairs as input and generates a string representation of the path parameters for an API request.

Example Usage:

getParam := map[string]string{
	"key1": "value1",
	"key2": "value2",
}

pathParams := GeneratePathParam(getParam)
fmt.Println(pathParams)

Types

type Request

type Request struct {
	Details    string                 // Details is the description of the test.
	Url        string                 // Url is the URL of the API.
	Method     string                 // Method is the HTTP method of the API.
	BodyType   interface{}            // BodyType is the type of the body of the API.
	BodyParam  interface{}            // BodyParam is the body of the API.
	QueryParam interface{}            // QueryParam is the query of the API.
	Header     map[string]interface{} // Header is the header of the API.

	ExpectedStatus int // ExpectedStatus is the expected status of the API.
}

Request is the struct for the request.

type Result

type Result struct {
	Status      bool
	Description string
	Error       interface{}
	Time        time.Duration
}

Result is the struct for the result of the test.

type Test

type Test struct {
	TotalTests  int64
	PassedTests int64
	FailedTests int64
	Result      map[int64]Result
	Server      *httptest.Server
	ServerMux   *http.ServeMux
}

Test is the struct for the test.

func Initialize

func Initialize() *Test

Initialize initializes the test.

This returns a pointer to a Test struct. The function initializes the struct with default values and creates a new HTTP server using httptest.NewServer.

Example Usage:

test := Initialize()

func (*Test) Add

func (h *Test) Add(httpReq Request)

Add method is a function that adds a new test case to the Test struct. It sends an HTTP request to a specified URL with the given parameters, and then checks the response against the expected status code.

Example Usage:

test := Test{}

test.Add(Request{
	Details:        "Test Case 1",
	Url:            "/api/test",
	Method:         "GET",
	BodyType:       "application/json",
	BodyParam:      nil,
	QueryParam:     nil,
	Header:         nil,
	ExpectedStatus: 200,
})

func (*Test) Dump

func (h *Test) Dump(exit bool)

Dump function method is responsible for printing the test results in a formatted manner.

Response time status: - **Best**: Optimal performance, ideal user experience, and minimal latency [0 - 100 ms] - **Good**: Acceptable performance with minor delays, generally not noticeable to users [100 - 300 ms] - **Acceptable**: Noticeable delays, but still within acceptable limits for most users [300 - 1000 ms (1s)] - **Poor**: Significant delays, negatively impacting user experience [1 - 2 seconds] - **Worst**: Unacceptable performance, leading to frustration and potential abandonment of the application [> 2 seconds] - **Invalid**: Errors or timeout conditions where the API fails to respond within a reasonable time frame [Timeout or Error]

Example Usage:

test.Dump(true)

Jump to

Keyboard shortcuts

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