dapr / go-sdk

Dapr SDK for go
Apache License 2.0
443 stars 170 forks source link

How to properly test #494

Open qustavo opened 8 months ago

qustavo commented 8 months ago

I'm opening this issue to ask how should I test components that are using dapr. In my case, I needed to make sure that my code was using pubsub correctly. I ended up exec.Command("dapr", "run", ... more args ...) and creating a Client and a Service that conneted to the dapr daemon. While this worked, I wonder if there is a programmatic way to do this. I skim through this repo and dapr/dapr and I could find anything. Is this the right way to test? is there a better one?

Thanks!

etiennetremel commented 7 months ago

Depends what test you are looking for. For my case, I used testcontainers to spin-up containers and test Dapr functionalities during the integration testing phase. It's particularly useful when a service provider doesn't offer a way to test their proprietary service locally. With Dapr it's just a matter of swapping a container with an open-source alternative.

You can find an example in the following repository: https://github.com/etiennetremel/testcontainers-dapr-example

qustavo commented 7 months ago

I'm following a similar approach but without docker, just by running the binnary (os.Exec). Using docker sounds interesting, portable and contained, specially when it comes to finding unused ports. Although it would be nice to have some package implementing a testing server. Have you experimented creating a grpc server for test?

etiennetremel commented 7 months ago

I haven't played with GRPC for tests, but I wouldn't expect much difference from a standard HTTP server

qustavo commented 7 months ago

have you started a HTTP server for tests?

mikeee commented 7 months ago

The examples in this repo are validated using mechanical markdown - https://github.com/dapr/mechanical-markdown Have a look at the validation github workflow along with the README files which validates the output against expected.