I have a small service written in Go. I'm already testing it with httptest
et al, but, I'm mocking the database and etc...
What I would like to do:
The empty database part is not a problem, since I made everything configurable via environment variables.
Make requests to it is also not the problem, as it is just standard Go code...
The problem is: I don't know how to start the server in a way that I could measure the coverage of it (and it's sub-packages). Also, the main server code is inside a main
function... I don't even know if I can call it from somewhere else (I tried the standard way, but not with reflection and stuff like that).
I'm kind of new using Go, so, this I might be talking nonsense.
Integration testing tests integration or interfaces between components, interactions to different parts of the system such as an operating system, file system and hardware or interfaces between systems. Integration testing is a key aspect of software testing.
API testing flow is quite simple with three main steps: Send the request with necessary input data. Get the response having output data. Verify that the response returned as expected in the requirement.
Postman can be used to automate many types of tests including unit tests, functional tests, integration tests, end-to-end tests, regression tests, mock tests, etc. Automated testing prevents human error and streamlines testing.
You can start the http server in your test, and make requests against it.
For more convenience, you can use httptest.Server
in the test, and give it your primary http.Handler. The httptest.Server
has some methods to better control to start and stop the server, and provides a URL
field to give you the local address of the server.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", greeting)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With