Etcd: How to get a mock Etcd for unit test?

Created on 6 May 2020  路  4Comments  路  Source: etcd-io/etcd

I want to use etcd to test my program, and I need a mock etcd (used it in my unit test). However, I'm confused that which API I should use. After I read the document of the etcd, I found that there may be two options for me.

  1. start a cluster like the example in /embed/doc.go, then use the client to connect it.
  2. start a cluster and get the client by integration.NewClusterV3
    It seems that option two is convenient. However, the document says it's used in internal testing, and I'm not sure is it ok to use it in my unit test.

Can anyone give me some advice? Thanks.

stale

Most helpful comment

use testify library to create mock of KV, TXN,WATCHER ....etc

            etcdFn: func() *clientv3.Client {
                resp := &clientv3.GetResponse{}
                kv := &mocks.KV{}
                kv.On("Get", mock.Anything, "/leader").Return(resp, nil)
                return &clientv3.Client{KV: kv}
            }

like this to mock etcd client @wjhuang2016

All 4 comments

use testify library to create mock of KV, TXN,WATCHER ....etc

            etcdFn: func() *clientv3.Client {
                resp := &clientv3.GetResponse{}
                kv := &mocks.KV{}
                kv.On("Get", mock.Anything, "/leader").Return(resp, nil)
                return &clientv3.Client{KV: kv}
            }

like this to mock etcd client @wjhuang2016

Hi, @dongzerun.
Thank you for your reply, however, it's not convenient to use the testify library. I want to do many operations like put, get, and watch. It's tough to mock every response. What I need is an etcd cluster that behaves like real etcd cluster and providing some functionality and good performance.

unit test shouldn't rely on thirdpart services, testify is enough for your case

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 21 days if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings