Tutorial by Examples

main.go: package main import ( "fmt" ) func main() { fmt.Println(Sum(4,5)) } func Sum(a, b int) int { return a + b } main_test.go: package main import ( "testing" ) // Test methods start with `Test` func TestSum(t *testing.T) { go...
If you want to measure benchmarks add a testing method like this: sum.go: package sum // Sum calculates the sum of two integers func Sum(a, b int) int { return a + b } sum_test.go: package sum import "testing" func BenchmarkSum(b *testing.B) { for i := 0; i < ...
This type of testing is popular technique for testing with predefined input and output values. Create a file called main.go with content: package main import ( "fmt" ) func main() { fmt.Println(Sum(4, 5)) } func Sum(a, b int) int { return a + b } After you r...
This type of tests make sure that your code compiles properly and will appear in the generated documentation for your project. In addition to that, the example tests can assert that your test produces proper output. sum.go: package sum // Sum calculates the sum of two integers func Sum(a, b in...
main.go: package main import ( "fmt" "io/ioutil" "log" "net/http" ) func fetchContent(url string) (string, error) { res, err := http.Get(url) if err != nil { return "", nil } defer res.Body.Clo...
This example shows how to mock out a function call that is irrelevant to our unit test, and then use the defer statement to re-assign the mocked function call back to its original function. var validate = validateDTD // ParseXML parses b for XML elements and values, and returns them as a map of ...
You can set a setUp and tearDown function. A setUp function prepares your environment to tests. A tearDown function does a rollback. This is a good option when you can't modify your database and you need to create an object that simulate an object brought of database or need to init a configu...
Run go test as normal, yet with the coverprofile flag. Then use go tool to view the results as HTML. go test -coverprofile=c.out go tool cover -html=c.out

Page 1 of 1