DEV Community

Cover image for Creating a Go Module & Writing Tests in Less Than 3 Minutes
Adron Hall
Adron Hall

Posted on

Creating a Go Module & Writing Tests in Less Than 3 Minutes

In this short (less than 4 minutes) video I put together a Go module library, then setup some first initial tests calling against functions in the module.

NOTE: For each of these time points listed below, if you navigate to the video on YouTube, the time points are easier to naviagte to since YouTube creates clickable time points! 👍🏻

00:12 - Writing unit tests. 📝
00:24 - Create the first go file.
00:40 - Creating the Go go.mod file. When creating the go.mode file, note the command is go mod init [repopath] where repopath is the actual URI to the repo. The go.mod file that is generated would look like this.

module github.com/adron/awesomeLib

go 1.13
Enter fullscreen mode Exit fullscreen mode

00:56 - Enabling Go mod integration for Goland IDE. This dialog has an option to turn off the proxy or go direct to repo bypassing the proxy. For more details on the Go proxy, check out goproxy.io and the Go docs for more details.
01:09 - Instead of TDD, first I cover a simple function implementation. Note the casing is very important in setting up a test in Go. The test function needs to be public, thus capitalized, and the function needs accessible, thus also capitalized. The function in the awesomeLib.go file, just to have a function that would return some value, looks like this.

package awesomeLib

func GetKnownResult() string {
    return "known"
}
Enter fullscreen mode Exit fullscreen mode

01:30 - Creating the test file, awesomeLib_test.go, then creating the test. In this I also show some of the features of the Goland IDE that extracts and offers the function names of a module prefaced with Test and other naming that provides they perform a test, performance, or related testing functionality.

package awesomeLib

import "testing"

func TestGetKnownResult(t *testing.T) {
    got := GetKnownResult()
    if got != "known" {
        t.Error("Known result not received, test failed.")
    }
}
Enter fullscreen mode Exit fullscreen mode

02:26 - Running the standard go test to execute the test. To run tests for all files within this module, such as if we've added multiple directories and other files, would be go test ./....
02:36 - Using Goland to run the tests with singular or multiple tests being executed. With Goland there are other capabilities to show test coverage, what percentage of functionality is covered by tests, and other various code metrics around testing, performance, and other telemetry.

That's it, now the project is ready for elaboration and is setup for a TDD, BDD, or implement and test style approach to development.

For JavaScript, Go, Python, Terraform, and more infrastructure, web dev, and coding in general I stream regularly on Twitch at https://twitch.tv/adronhall, post the VOD's to YouTube along with entirely new tech and metal content at https://youtube.com/c/ThrashingCode.

For more blogging, I've got https://compositecode.blog and the Thrashing Code Newsletter, sign up for it here!

Discussion (0)