简体   繁体   English

测试课程

[英]testing classes

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. 我开始进行alpha测试,并且在某些时候意识到我正在为类本身添加与alpha测试相关的方法。 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. 我正在寻找的是专门的alpha测试/单元测试类的技术,而不会增加被测试类的重量。

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 sounds like you don't want Unit testing, which is correctly the verification that the interface of a class works. 听起来你不想要单元测试,这正是一个类接口工作的验证。 You shouldn't have to change your class at all in order to do unit testing. 你不应该为了进行单元测试而改变你的课程。 If you are looking for a way to verify the internal state of your object so that it remains consistent, you should look into Design by Contract methods, which can verify internal state from within the object. 如果您正在寻找一种方法来验证对象的内部状态以使其保持一致,那么您应该查看“ 按合同设计”方法,该方法可以验证对象内部的状态。

Those are good. 那些都很好。 I have usually also wanted the test class to not only be spearate from the original, but also in a complete different DLL/EXE, as well as testing the "real" compiled class from the "real" DLL/EXE into which it was compiled. 我通常也希望测试类不仅是原始的,而且是完全不同的DLL / EXE,以及从编译它的“真正的”DLL / EXE中测试“真正的”编译类。 。

The one additional technique I've found is to re-define the class within the testing tool. 我发现的另一种技术是在测试工具中重新定义类。 Copy the class definition exactly, but make everything public. 完全复制类定义,但将所有内容都公开。 This allows the test tool to have 'white-box' access to the class, but the actual implementation is still from the real class code. 这允许测试工具对类进行“白盒”访问,但实际的实现仍然来自真正的类代码。

ie

class myClass
{
private:
    int foo;
public:
    myClass() { foo = 0; }
}

and then: 接着:

class test_myClass
{
public:
    int foo;
public:
    test_myClass();
};

void test()
{
    myClass *c = new myClass();
    test_myClass *t = (test_myClass*)c;
    // All methods are called on c.
    // White-box access is available through t.
};

Oh... and DevStudio 2008 now has some really cool unit testing capabilities, including the ability to declare a 'friend' assembly , which allows white-box access to all your internal classes in the assembly being tested. 哦......而DevStudio 2008现在有一些非常酷的单元测试功能,包括声明一个'朋友' 程序集的功能 ,它允许白盒访问正在测试的程序集中的所有内部类。

I have done a lot of framework building - basically calling the classes/interfaces I wanted to test. 我做了很多框架构建 - 基本上调用我想测试的类/接口。 There was no additional work in the classes. 班上没有额外的工作。

I also built classes a few times that made the public methods virtual. 我还建了几次类,使公共方法变得虚拟。 I derived from those and made test classes/objects. 我从那些派生出来并制作了测试类/对象。 The test object methods called the parent (real class) methods and also logged all calls and results. 测试对象方法称为父(真实类)方法,并记录所有调用和结果。 This was more for logging than testing, but it worked as well. 对于日志记录而言,这比测试更多,但它也有效。

The methods above I did before all the hype about unit testing and the like. 在关于单元测试之类的所有宣传之前,我做了上述方法。 (circa late 1990s) (大约1990年代后期)

It worked well for me then, but I have not done too much with the Junit/nunit stuff and am eager to actually give them a whirl on real projects. 那时它对我来说效果很好,但是我对Junit / nunit的东西并没有做太多的事情,并且我真的很想让它们在实际项目上发挥作用。

sample for one method 一种方法的样本

class Thing 上课

{... {...

public: virtual DoStuff(); public:virtual DoStuff(); . .. }; ..};

class ThingTest : public Thing ThingTest:public Thing

{ {

virtual DoStuff() 虚拟DoStuff()

{ {

//log the call and the parameters. //记录呼叫和参数。

// make the call to the parent //拨打父母

// log the return value //记录返回值

// return the return value //返回返回值

} }

}; };

eJames is right Unit Testing needs to focus on Interface that the inputs into the Class are producing the correct Output. eJames是正确的单元测试需要关注接口,即类的输入产生正确的输出。 Any private or friend variable are part of the implementation and are not tested specifically. 任何私有或朋友变量都是实现的一部分,未经过特定测试。

If you had made an error in a private routine to the class then it would show up incorrect output. 如果您在类的私有例程中出错,那么它将显示错误的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM