Testing microservices?

I know this question is a little subjective, but I'm at a loss as to what to do here. I am currently using Go + Go-kit to write some microservices. I would like to test the endpoints of these microservices on models like an integration test, but I'm not sure how. The only thing I can think of is to have shell scripts that hit the endpoints and check for responses. But that sounds like a kludge, not a real smart practice. I feel like there must be a better way to do this. Anyone have any suggestions?

+3


source to share


3 answers


An alternative approach to end-to-end testing is Consumer Driven Contract (CDC).

While it is useful to have some end-to-end tests, they have some drawbacks such as:

  • the consumer service needs to know how to start the provider service. This sounds like unnecessary information that is likely to be difficult to maintain as the number of services increases,

  • service startup can be slow. Even speaking for only a few seconds, it adds a time-generating overhead. If a consumer is dependent on multiple services, it all starts to add up,

  • the provider service may be dependent on the data store or other services working as expected. This means that now it is necessary to start not only the provider, but also several other services, possibly a database.



The CDC's idea is described shortly:

  • The user defines what he expects from a particular service request
  • The supplier and consumer agree to this contract
  • The supplier constantly checks that the contract has been fulfilled.

This information is taken from here . Read more about this article, it can be helpful even if it is Java specific.

0


source


You can do this in a standard Go unit test using httptest . This allows you to create mocks Request

and ResponseWriter

objects that can be passed by anyone Handler

or HandleFunc

. You create an appropriate one Request

, pass it to a handler, then read the response from ResponseRecorder

and test it against the expected response.



If you are using the default multiplex (call http.Handle()

to register handlers), you can test it with http.DefaultServeMux

. I've used it for microservices in the past with good results. Works for benchmark handlers, routing and middleware.

0


source


You should always use the golang native unit testing framework to test every single service (no shell script please!). httptest seems fine, but I'd say it's helpful to have finer test boundaries - you actually have one _test.go for each function block in your code. Smaller tests are easier to maintain.

For generic integration tests that involve multiple microservices, you shouldn't do them at design time. Set up a staging area and run tests there.

My 2 cents.

0


source







All Articles