Run Got automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package example_test
import (
"net/http"
"net/url"
"testing"
"github.com/ysmood/got"
"github.com/ysmood/got/lib/example"
)
type mockResponseWriter struct {
got.Mock
}
func (m *mockResponseWriter) Write(b []byte) (int, error) {
return m.Proxy("Write").(func([]byte) (int, error))(b)
}
func (m *mockResponseWriter) Header() http.Header { return nil }
func (m *mockResponseWriter) WriteHeader(c int) {}
func TestMocking(t *testing.T) {
g := setup(t)
m := &mockResponseWriter{}
m.Stub("Write", func(b []byte) (int, error) {
g.Eq(string(b), "3")
return 0, nil
})
u, _ := url.Parse("?a=1&b=2")
example.ServeSum(m, &http.Request{URL: u})
m.On(m, "Write").When([]byte("3")).Return(1, nil)
example.ServeSum(m, &http.Request{URL: u})
}
package example
import (
"fmt"
"net/http"
"strconv"
)
// Sum a and b as number
func Sum(a, b string) string {
an, _ := strconv.ParseInt(a, 10, 32)
bn, _ := strconv.ParseInt(b, 10, 32)
return fmt.Sprintf("%d", an+bn)
}
// ServeSum http handler function
func ServeSum(w http.ResponseWriter, r *http.Request) {
s := Sum(r.URL.Query().Get("a"), r.URL.Query().Get("b"))
_, _ = w.Write([]byte(s))
}
package example_test
import (
"testing"
"github.com/ysmood/got/lib/example"
)
func TestChainMethods(t *testing.T) {
g := setup(t)
g.Desc("1 must equal 1").Must().Eq(example.Sum("1", "2"), "3")
}
func TestUtils(t *testing.T) {
g := setup(t)
// Run "go doc got.Utils" to list available helpers
s := g.Serve()
s.Mux.HandleFunc("/", example.ServeSum)
val := g.Req("", s.URL("?a=1&b=2")).Bytes().String()
g.Eq(val, "3")
}
func TestTableDriven(t *testing.T) {
testCases := []struct{ desc, a, b, expected string }{{
"first",
"1", "2", "3",
}, {
"second",
"2", "3", "5",
}}
for _, c := range testCases {
t.Run(c.desc, func(t *testing.T) {
g := setup(t)
g.Eq(example.Sum(c.a, c.b), c.expected)
})
}
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.