Unit Testing
This is the test file. It should be placed in the same directory as
your application and name main_test.go
.
package main
import (
"net/http"
"net/http/httptest"
"testing"
func Test_HelloWorld(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
t.Fatal(err)
}
HelloWorld(res, req)
exp := "Hello World"
act := res.Body.String()
if exp != act {
t.Fatalf("Expected %s gog %s", exp, act)
}
- Change the output of
HelloWorld
to print a parameter and then test that the parameter is rendered. - Create a POST request and test that the request is properly handled.