Skip to content

API Mocks #1438

Open
Open
@AmitThakkar

Description

@AmitThakkar

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,

Activity

ArthurSens

ArthurSens commented on Feb 2, 2024

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

AmitThakkar commented on Feb 6, 2024

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

zchenyu commented on Feb 6, 2024

@zchenyu

+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

bwplotka commented on Feb 6, 2024

@bwplotka
Member

👋🏽

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

zchenyu commented on Feb 6, 2024

@zchenyu

@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

satokenta940 commented on May 30, 2024

@satokenta940

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.



linked a pull request that will close this issue on Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @zchenyu@AmitThakkar@bwplotka@ArthurSens@satokenta940

      Issue actions

        API Mocks · Issue #1438 · prometheus/client_golang