package mypkg_test import ( "fmt" "testing" "launchpad.net/gocheck" ) func Test(t *testing.T) { gocheck.TestingT(t) } type MySuite struct{} var _ = gocheck.Suite(&MySuite{}) func (s *MySuite) TestSomething(c *gocheck.C) { fmt.Println("TestSomething") c.Assert(1+1, gocheck.Equals, 2) }
package httptest import ( "bytes" "io/ioutil" "net/http" "testing" "launchpad.net/gocheck" ) type HTTPSuite struct { Server *http.Server URL string } var _ = gocheck.Suite(&HTTPSuite{}) func Test(t *testing.T) { gocheck.TestingT(t) } func (s *HTTPSuite) SetUpTest(c *gocheck.C) { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("Hello, World!")) }) s.Server = &http.Server{Addr: ":8080", Handler: mux} go s.Server.ListenAndServe() s.URL = "http://localhost:8080/" } func (s *HTTPSuite) TestRequest(c *gocheck.C) { res, err := http.Get(s.URL) c.Assert(err, gocheck.IsNil) c.Assert(res.StatusCode, gocheck.Equals, http.StatusOK) body, err := ioutil.ReadAll(res.Body) c.Assert(err, gocheck.IsNil) c.Assert(string(body), gocheck.Equals, "Hello, World!") } func (s *HTTPSuite) TearDownTest(c *gocheck.C) { s.Server.Close() }This example demonstrates the use of gocheck package for testing HTTP servers in Go. It defines an HTTP test suite with a `SetUpTest` and a `TearDownTest` method that sets up and tears down an HTTP server. The test case `TestRequest` makes a GET request to the server and asserts the response status code, body, and other properties using gocheck assertions. In conclusion, launchpad.net/gocheck is a package library for testing in Go language. It provides a convenient and easy-to-use framework for writing and executing tests in Go. The above examples demonstrate the use of gocheck for basic and HTTP testing scenarios respectively.