Skip to content

Instantly share code, notes, and snippets.

@spilth
Last active August 3, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spilth/e163a09b8e502559a3a66bec3513732a to your computer and use it in GitHub Desktop.
Save spilth/e163a09b8e502559a3a66bec3513732a to your computer and use it in GitHub Desktop.
Unit Testing in Go

Unit Testing in Go

Install Go

$ brew install golang
$ export GOPATH="$HOME/go"
$ export GOBIN="$GOPATH/bin"
$ export PATH="$GOBIN:$PATH"

Set Up Project

$ mkdir -p ~/go/src/github.com/spilth/ogay
$ cd ~/go/src/github.com/spilth/ogay
$ touch ogay_test.go

Using Go's Testing Package

Following https://golang.org/pkg/testing/

ogay_test.go

package ogay

import "testing"

func TestWordsStartingWithAVowel(t *testing.T) {
	result := TranslateWord("apple")

	if result != "appleway" {
		t.Errorf("apple translated to `%s`, not `appleway`", result)
	}
}

ogay.go

package ogay

func TranslateWord(englishWord string) string {
  return englishWord + "way"
}

Success Output

PASS
ok  	github.com/spilth/ogay	0.006s

Failure Output

--- FAIL: TestWordsStartingWithAVowel (0.00s)
	ogay_test.go:9: apple translated to `appleway`, not `appleways`
FAIL
exit status 1
FAIL	github.com/spilth/ogay	0.006s

Using Testify

Library Installation

$ go get github.com/stretchr/testify

ogay_test.go

package ogay

import (
	"testing"
	"github.com/stretchr/testify/assert"
)

func TestWordsStartingWithAVowel(t *testing.T) {
	result := TranslateWord("apple")

	assert.Equal(t, "appleways", result)
}

Success Output

PASS
ok  	github.com/spilth/ogay	0.014s

Failure Output

--- FAIL: TestWordsStartingWithAVowel (0.00s)
	ogay_test.go:11:
			Error Trace:	ogay_test.go:11
			Error:      	Not equal:
			            	expected: "appleways"
			            	actual  : "appleway"

			            	Diff:
			            	--- Expected
			            	+++ Actual
			            	@@ -1 +1 @@
			            	-appleways
			            	+appleway
			Test:       	TestWordsStartingWithAVowel
FAIL
exit status 1
FAIL	github.com/spilth/ogay	0.013s

Using Ginkgo

Library Installation & Setup

$ go get github.com/onsi/ginkgo/ginkgo
$ go get github.com/onsi/gomega/...
$ ginkgo bootstrap
$ rm ogay_test.go
$ ginkgo generate ogay # generates ogay_test.go

ogay_test.go

package ogay_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/spilth/ogay"
)

var _ = Describe("Ogay", func() {
	Describe("Translating words", func() {
		Context("Words starting with a vowel", func() {
			It("translated them correctly", func() {
				Expect(ogay.TranslateWord("apple")).To(Equal("appleways"))
			})
		})
	})
})

Success Output

Running Suite: Ogay Suite
=========================
Random Seed: 1533304301
Will run 1 of 1 specs

•
Ran 1 of 1 Specs in 0.000 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok  	github.com/spilth/ogay	0.015s

Failure Output

Running Suite: Ogay Suite
=========================
Random Seed: 1533304334
Will run 1 of 1 specs

• Failure [0.000 seconds]
Ogay
/Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:9
  Translating words
  /Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:10
    Words starting with a vowel
    /Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:11
      translated them correctly [It]
      /Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:12

      Expected
          <string>: appleway
      to equal
          <string>: appleways

      /Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:13
------------------------------


Summarizing 1 Failure:

[Fail] Ogay Translating words Words starting with a vowel [It] translated them correctly
/Users/bkelly/go/src/github.com/spilth/ogay/ogay_test.go:13

Ran 1 of 1 Specs in 0.001 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped
--- FAIL: TestOgay (0.00s)
FAIL
exit status 1
FAIL	github.com/spilth/ogay	0.015s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment