prometheus / client_golang

Prometheus instrumentation library for Go applications
https://pkg.go.dev/github.com/prometheus/client_golang
Apache License 2.0
5.44k stars 1.19k forks source link

API Mocks #1438

Open AmitThakkar opened 10 months ago

AmitThakkar commented 10 months ago

Hello,

I am using this library to interact with Prometheus and realized the mocks are missing for the API interface.

Is there any plan to provide mocks so users can morally write tests?

Thanks,

ArthurSens commented 10 months ago

Is there any plan to provide mocks so users can morally write tests?

I don't think there was any discussion around this. Do you have examples of other libraries doing the same?

I'm not against the idea 🤔

AmitThakkar commented 10 months ago

Hello @ArthurSens,

Thanks for looking into my request.

Here are a few examples:

Not all libraries include a mocks directory with pre-made mock implementations. However, many Go libraries follow a convention where they include mock implementations alongside their regular codebase, often in a mock directory or a similarly named one.

zchenyu commented 9 months ago

+1 I was just looking for this You can use a tool like https://github.com/uber-go/mock to easily auto-gen it

bwplotka commented 9 months ago

👋🏽

Interesting topic. I am not immediately opposed, but wanted to give perspective on why generally maintaining mocks next to the backend API tend to be controversial topic:

Initially, when I started working with Go I was mocking fun. But with time, it's just not productive to mock everything and every detail, unless you write software for NASA.

I would be more curious if a simple "Prometheus fake" would be a better use of our time e.g. where somebody could combine client_golang Prometheus API client with some code that allows injecting a simple array of series and PromQL responses.

zchenyu commented 9 months ago

@bwplotka Those are good points. Fakes are indeed preferable to mocks when possible. I also see your point about there not being a standard mocking framework, that indeed throws a wrench into things.

satokenta940 commented 6 months ago

Apidog may be able to solve your problem,is a free platform or tool that allows you to import an API definition and generate mock data for testing and development purposes. Its "Smart Mock" feature can generate mock data automatically based on the names of fields, allowing you to create realistic-looking mock data without having to manually specify the values for each field.