Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Mocks #1438

Open
AmitThakkar opened this issue Jan 29, 2024 · 5 comments
Open

API Mocks #1438

AmitThakkar opened this issue Jan 29, 2024 · 5 comments

Comments

@AmitThakkar
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link

zchenyu commented Feb 6, 2024

+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
Copy link
Member

bwplotka commented Feb 6, 2024

👋🏽

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:

  • Orgs/projects who wants to test against mocks prefer their own style of mock methods/libraries for testing consistency (e.g. against other deps).
  • Coding API is one thing, but there are tons of semantics not coded via interfaces. For this fakes (or real e2e deps) are better. For example with the docker and e2e test framework, it's trivial to literally run Prometheus for your integration test.
  • Mocks are prone to other problems like testing wrong things, being too fragile or too brittle.
  • Why maintaining and hosting the mocking code, if it's trivial to auto-generate this code (e.g. with go generate command).

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
Copy link

zchenyu commented Feb 6, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants