Created
July 12, 2018 16:10
-
-
Save deankarn/0adf3de659016b1871555fa0615bdc1d to your computer and use it in GitHub Desktop.
tests for the do things Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib | |
import ( | |
"testing" | |
"os" | |
"github.com/stretchr/testify/require" | |
) | |
// TestMain isn't actually necessary to be in the test file, just here to demonstrate | |
// setup + teardown in Go's STD testing | |
func TestMain(m *testing.M) { | |
// this is the place for setup + teardown for all tests | |
// for individual test setup + teardown, that can be done in each test individually. | |
// for shared setup/teardown just create some helper functions. | |
// call flag.Parse() here if TestMain uses flags | |
os.Exit(m.Run()) | |
} | |
func TestDoThings(t *testing.T) { | |
tests := []struct { | |
name string | |
input string | |
errorExpected bool | |
}{ | |
{ | |
name: "test 1", | |
input: "thing 1", | |
errorExpected: false, | |
}, | |
{ | |
name: "test 2", | |
input: "thing 2", | |
errorExpected: false, | |
}, | |
{ | |
name: "test 3", | |
input: "Cat in the Hat", | |
errorExpected: true, | |
}, | |
} | |
for _, tt := range tests { | |
tc := tt // need to capture because going to be running in parallel | |
t.Run(tt.name, func(t *testing.T) { | |
t.Parallel() | |
err := DoThings(tc.input) | |
if err == nil == tc.errorExpected { | |
t.Fatal() | |
} | |
}) | |
} | |
} | |
func TestDoThingsLongform(t *testing.T) { | |
assert := require.New(t) | |
err := DoThings("thing 1") | |
assert.NoError(err) | |
err = DoThings("thing 2") | |
assert.NoError(err) | |
err = DoThings("Cat in the Hat") | |
assert.Error(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment