<p>Hi all, </p>
<p>I'm currently making a <a href="https://github.com/sirkaiserkai/go-diawi/tree/master" rel="nofollow">package</a> that wraps the client functionality for a web service called <a href="https://www.diawi.com/" rel="nofollow">diawi</a>. For background, diawi's is a hosting site for mobile apps in development or to be released to a limited audience (Such as testers, clients, etc). </p>
<p>I've completed most of the package and want to begin writing tests for all the functionality and I've hit a (mental) road block, how exactly should I write tests for a client side of a web service? </p>
<p>Looking at upload_test.go</p>
<pre><code>package godiawi
import (
"testing"
)
func TestUploadSuccess(t *testing.T) {
ur := UploadRequest(Token: "APIToken", File: "app.ip"}
uploadResponse, err := ur.upload()
if err != nil {
t.Errorf("Error: %s", err)
}
if uploadResponse.status != Ok {
t.Errorf("Failed to upload app)
}
}
</code></pre>
<p>Would this be a bad design since this is dependent on the web service? Since if errors or issues are occurring on diawi's side, it would then report a false-negative in the test cases.</p>
<p>I would like to avoid this but what is the recommended way of making the tests deterministic? </p>
<hr/>**评论:**<br/><br/>fvosberg: <pre><p>I usually do this by first, writing tests against the third party service which consist of more or less a request/expected response pair. This tests I just have to execute while writing it and in the future to verify the API hasn't changed. After that, I write a mock for the external service and use the same tested pairs of request/response. This mock is ready to be used in your test. But that trades only off, when the client itself has logic, I think. You can't test that an external service has not changed it's behavior without relying on it's availability</p></pre>fvosberg: <pre><p>Another good way I've found is this recorder: <a href="https://github.com/seborama/govcr" rel="nofollow">https://github.com/seborama/govcr</a>
I didn't use it yet, but it looks like the thing you need, doesn't it?</p></pre>adelowo: <pre><p>You should make use of the request recorder <a href="https://godoc.org/net/http/httptest#ResponseRecorder" rel="nofollow">https://godoc.org/net/http/httptest#ResponseRecorder</a></p></pre>sethammons: <pre><p>Have the client to diawi adhere to an interface. Then you use a fake that adheres to the interface in your tests. You can now test your code against errors and happy path from diawi. Like you said, you don't want to be in a false-negative situation depending on their ability to serve your request. At least, not at the unit level. For integration testing, I think it is fine to make a real call to their service, but this is something that should happen at the end of testing, before deployment.</p></pre>bigpigfoot: <pre><p>you should just test your requests (for client) and responses (for server), then proceed to integration tests.</p>
<p>for example, I'd do </p>
<pre><code>func TestUploadRequest(t *testing.T) {
req, err := client.UploadRequest(params)
if err != nil {
t.Error(err)
}
assert(req)
}
</code></pre></pre>puffybunion: <pre><p>Like others have said - you can just use ResponseRecorder.</p>
<p>Another option is mocking an http.Client by defining a Doer/Client interface: <a href="https://play.golang.org/p/P8fFoC58Cg" rel="nofollow">https://play.golang.org/p/P8fFoC58Cg</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传