测试类 [英] testing classes

查看:87
本文介绍了测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天把一个班放在一起做一些有用的任务。我开始alpha测试,并在某一点上意识到我正在添加alpha测试相关的方法到类本身。它打我,他们不属于那里。在头部划伤后,我从基类中导出了一个可以访问受保护成员的测试类。我把所有的测试相关的方法,并在测试类中设置和删除,并留下基础类瘦和平均为古语。



在这里浏览一段时间我发现一个评论,建议使用这种技术,使测试类成为真正的类的朋友。



回想起来,这两种技术应该



我正在寻找的是特定alpha测试/单元测试类的技术,不增加被测试类的重量

h2_lin>解决方案

单元测试的目标之一是验证接口到您的类。这意味着,一般来说,你不应该测试你的类的脏内脏。单元测试应该与您的类的公共输入和输出交互,并验证行为是否如预期。因此,您可以更改类的内部实现,而不会影响依赖它的所有其他对象。显然,我不知道你的情况的细节,但我会说,作为一个一般规则,如果你的单元测试是试图找出类的私人细节,你做错了。



编辑:另请参阅:这个SO问题。注意,它可以做(顶部答案),但也注意到第二个地方的答案(短的利润)或多或少说明我上面提到的相同的事情。


I put together a class yesterday to do some useful task. I started alpha testing, and at some point realized I was adding alpha test related methods to the class itself. It hit me that they don't belong there. After a bit of head scratching I derived a test class from the base class that has access to the protected members as well. I put all of the testing related methods, and set up and tear down in the test class, and left the base class lean and mean as the old saying goes.

After browsing around here awhile I found one comment that suggested using this sort of technique by making the testing class a friend of the real class.

In retrospect both of those techniques should have been obvious to me.

What I am looking for, are techniques for specifically alpha testing/unit testing classes, without adding to the weight of the class being tested.

What techniques have you personally used, and recommend?

解决方案

One of the goals of unit testing is to verify the interface to your classes. This means that, generally speaking, you shouldn't be testing the dirty innards of your class. The unit test is supposed to interact with the public inputs and outputs of your class, and verify that the behaviour is as expected. You are thus able to change the internal implementation of your class without affecting all of the other objects that depend on it. Obviously, I don't know the details in your situation but I would say that, as a general rule, if your unit test is trying to figure out the private details of the class, you are doing something wrong.

edit: See also: This SO question. Notice that it can be done (top answer), but also notice that the second-place answer (by a short margin) says more or less the same thing as I mention above.

这篇关于测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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