<p>I Have found that when writing unit tests, I often have a slice of an implicitly defined struct called <code>cases</code> where I have an input and an output, and later I have a for loop that iterates over said cases slice. I often have to type very simlilar code over many unit tests, which is bad practice from my understanding. Is there a library that generalises this function of having a slice of cases then iterating over it for unit tests?</p>
<hr/>**评论:**<br/><br/>pdffs: <pre><p>Your test cases are almost always of a specific type that relates to the code that you're testing though, so there's not really any way to abstract it sensibly.</p>
<p>There's also the adage: <em>A little copying is better than a little dependency.</em></p></pre>rwaweber: <pre><p>It's very possible that I'm misreading your question, but what it sounds like you're describing is kind of in the realm of the concept of <a href="https://github.com/golang/go/wiki/TableDrivenTests" rel="nofollow">table testing</a>.</p>
<p>That way you can illustrate test cases as data. I've found that it helps to iron out more edge cases as I write them, looking at them all grouped together.</p>
<p>A good, albeit a little complicated example I've been studying lately <a href="https://github.com/Shopify/sarama/blob/master/client_tls_test.go" rel="nofollow">here</a>.</p></pre>creikey: <pre><p>Yes, that's exactly what I'm describing! Thank you! I think I could easily turn this into a small library to make error messages more uniform. Thanks</p></pre>dlsniper: <pre><p>No, you can't, because you don't have generics in Go and because that library would have to accept an input and an output set of values, and all it would do would be to use a for loop and run the tests against the input value. Don't try to abstract the wheel when there's nothing to abstract.</p></pre>drvd: <pre><blockquote>
<p>which is bad practice</p>
</blockquote>
<p>Why should this be bad practice? I think uniformity of code is a good thing. Call this a "pattern" and have a good day. Nothing to worry here.</p></pre>creikey: <pre><p>If I want to change something small with the t.Errorf función, I have a lot of refactoring to do</p></pre>shovelpost: <pre><p>You can always use <code>got</code> and <code>want</code> or <code>have</code> and <code>want</code> as your variables. That way you don't have to change that much on refactoring.</p></pre>drvd: <pre><p>Now I'm confused. I have no idea how your original question is related to this reply. Probably you should show code.</p></pre>kostix: <pre><p>You might want to use <a href="https://github.com/stretchr/testify/" rel="nofollow"><code>github.com/stretchr/testify</code></a> then—basically it allows you to spill <code>assert.Whatever</code> instead of hand-crafted calls to <code>t.Errorf</code> as it has sensible idea about how to write what happened.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传