Create a gist now

Instantly share code, notes, and snippets.

@clairew /example_test.go Secret
Last active Aug 10, 2018

Embed
What would you like to do?
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