TDD:您为单元测试公开了哪些方法? [英] TDD: Which methods do you expose for unit testing?

查看:33
本文介绍了TDD:您为单元测试公开了哪些方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未完全理解 TDD 的一个方面.

There is one aspect of TDD which I never fully understood.

假设有人让你实现一个简单的 Stack 对象.如果您正确地完成了您的设计,您将获得一个非常简洁和干净的 API.假设:push()pop()isEmpty().除此之外的任何事情都会过度扼杀需求,并让用户有太多空间来弄乱您的代码.

Suppose somebody asked you to implement a simple Stack object. If you've done your design properly, you will come to a very minimal and clean API. Suppose: push(), pop() and isEmpty(). Anything more than that is over-killing the demand, and allowing the user too much room to mess with your code.

现在假设您要对代码进行单元测试.如果你所有的公共方法都只是上面显示的三个,你怎么做呢?到目前为止,这些方法仅适用于您的测试.

So now let's suppose you want to unit test your code. How do you go about doing this if all your public methods are just the three shown above? Those methods will take your testing only so far.

因此,您要么添加私有方法,这些方法根本对您没有帮助,因为它们对您的单元测试用例不可见.或者你将这些方法公开,然后你努力工作的简约 API 就会出现.现在用户会弄乱你的堆栈,错误肯定会出现.

So either you add private methods, which do not help you at all since they are not visible to your unit test case. Or you make those methods public and there goes your minimalistic API which you worked on so hard. Now the user is going to mess with your Stack and the bugs will be sure to come.

您如何处理开放公共测试方法与干净简单的 API 的困境?

How do you handle this dilemma of opening public methods for testing vs. a clean and simple API?

只是指出正确的方向,获得技术指导会很好(例如使用此黑客公开私有方法"等......)但我很更感兴趣的是关于这两个概念中哪一个更重要以及您如何处理这个主题的更通用的答案.

just to point in the right direction, It would be nice to get technical pointers (such as "use this hack to expose private methods", etc...) but I am much more intrested in more generic answers as to what of the two concepts is more important, and how you approach this subject.

推荐答案

  1. 测试功能;这通常意味着测试公共接口 - 因为不是所有功能都可以通过公共接口访问吗?如果它们不是,那么它们就不是特征!可能有例外,但我想不出任何例外.

  1. test the features; this usually means testing the public interface - for should not all features be accessible via the public interface? if they aren't, then they aren't features! There may be exceptions to this, but i can't think of any.

测试公共接口;任何不是从公共接口直接或间接调用的方法都没有必要.它们不仅不需要测试,而且根本不需要存在.

test the public interface; any methods that are not called directly or indirectly from the public interface are not necessary. Not only do they not need to be tested, they do not need to exist at all.

这篇关于TDD:您为单元测试公开了哪些方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆