Software testing is a process that evaluates the metrics of programs using tools, scripts, or manually.
Testing is an integral part of the software development cycle.
Comprehensive testing provides in-depth information on the integrity of your tool.
Getting Started With Testing in Go
The Go standard library provides an evolvingtestingpackage.
Thetestingpackage has benchmarking, fuzzing, skipping, sub-testing, sub-benchmarking, and other functionalities.
Testing with this package is easy.
Your test will test this function.
TheStringToIntegerfunction returns0if theres an error on conversion and the integer if there are no errors.
Heres a test function for StringToInteger:
TheTestStringToIntegertest function accepts atesting.Tobject as its argument.
TheexpectedIntvariable holds the result of the string conversion.
ThecaseInstancevariable is the instantiated Cases struct for the test.
Theifstatement compares the expected and actual values.
TheFailmethod returns a failed test in the else statement if the values arent equal.
Go provides atestcommand for automating and retrieving insights on your tests and programs.
Theres plenty of functionality beyond thetestingpackage, includingregression and unit testing.
The Testify Package
The Testify packageis one of the most popular Go frameworks for testing packages.
Testify is suitable for test-driven development since the package provides amockpackage.
This provides a mechanism for writing mock objects that you could use in place of real objects in testing.
TheEqualandNotEqualmethods are for equality and inequality-based assertions from Testifysassertpackage.
The GoConvey Package
GoConveyis a Go testing tool primed for expressivity over thetestingpackage.
It includesterminal (CLI) and surfing app (GUI)testing functionality.
The GoConvey package integrates with thetestingpackage, providing a web user interface for working with native Go tests.
It also includes functionality for regression tests, customizable outputs, and test code generation.
you might run tests automatically, access coverage formats in HTML, and customize the GUI.
Run this command in the terminal of your Go workspace to roll out the Go Convey package.
Heres a simple example of writing tests with the GoConvey package.
Youll need to import theconveypackage using dot notation for the test.
The Convey function from theConveypackage helps with test scoping.
The lastConveyfunction call in the code example asserts equality between thexvariable and2, using theShouldEqualfunction.
Thehttpexpectpackage is a set of chainable builders for HTTP requests and assertions on HTTP responses and payloads.
Its built on thehttp,testing, and other packages.
The package also works well with the built-inhttptestpackage.
httpexpectprovides functionality for request building with URL construction, headers, cookies, and payloads.
It handles response assertions, payload assertions, pretty printing, and WebSockets.
Run this command in the terminal of your working directory to install thehttpexpectpackage.
Heres a simple example of testing a handler function with thehttpexpectpackage.
TheexampleHandlerhandler function returns an HTTP handler for thehttpexpectpackage.
TheTestexampleHandlerfunction declares an instance of the handler function.
It then creates a new server for testing the endpoint with thehttptestpackage.
Theexpectvariable is yourhttpexpectinstance that hits theGETrequest endpoint root path on the server.
TheStatusfunction returnsthe status code(in this case,200) if the test succeeds.