Skip to content

Instantly share code, notes, and snippets.

@clairew
Last active August 10, 2018 16:58
Show Gist options
  • Save clairew/7d31df6f40f0af6d8ec4a3e948239b97 to your computer and use it in GitHub Desktop.
Save clairew/7d31df6f40f0af6d8ec4a3e948239b97 to your computer and use it in GitHub Desktop.
Example test to demonstrate helpers and fixtures
package example_tests
import (
"testing"
log "github.com/sirupsen/logrus"
)
//TestDBExample is an example test
func TestDBExample(t *testing.T) { // describe what the test is for and its scenarios
db, err := GetTestDBConnection()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Warn("Failed to initialize test database")
t.Fatalf("failed to initialize test database connection. Error output: %v", err)
}
defer TestDBTearDown(db)
t.Log("Testing DB for example functionality given valid input should return expected output")
validCondition := getValidInputExampleFunction() // provide necessary input for test
expectedOutput := testFunctionExpectedOutput()
actualOutput, err := ExampleFunction(validCondition, db)
if expectedOutput != actualOutput {
t.Fatalf("Expected output: %v, actual output: %v", expectedOutput, actualOutput)
}
t.Log("Testing DB for example functionality given valid input should return error")
invalidCondition := getInvalidInputExampleFunction()
actualOutput, err = ExampleFunction(invalidCondition, db)
if actualOutput != nil && err == nil {
t.Fatalf("ExampleFunction failed to return error given invalid input. Actual Output: %v", actualOutput)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment