如何对抽象类进行单元测试:使用存根扩展?

本文翻译自:How to unit test abstract classes: extend with stubs?

I was wondering how to unit test abstract classes, and classes that extend abstract classes. 我想知道如何对抽象类进行单元测试,以及扩展抽象类的类。

Should I test the abstract class by extending it, stubbing out the abstract methods, and then test all the concrete methods? 我应该通过扩展它来测试抽象类,删除抽象方法,然后测试所有具体方法吗? Then only test the methods I override, and test the abstract methods in the unit tests for objects that extend my abstract class? 然后只测试我覆盖的方法,并在单元测试中测试扩展我的抽象类的对象的抽象方法?

Should I have an abstract test case that can be used to test the methods of the abstract class, and extend this class in my test case for objects that extend the abstract class? 我是否应该有一个抽象的测试用例,可以用来测试抽象类的方法,并在我的测试用例中为扩展抽象类的对象扩展这个类?

Note that my abstract class has some concrete methods. 请注意,我的抽象类有一些具体的方法。


#1楼

参考:https://stackoom.com/question/11Hm/如何对抽象类进行单元测试-使用存根扩展


#2楼

I would argue against "abstract" tests. 我会反对“抽象”测试。 I think a test is a concrete idea and doesn't have an abstraction. 我认为测试是一个具体的想法,并没有抽象。 If you have common elements, put them in helper methods or classes for everyone to use. 如果您有共同的元素,请将它们放在帮助方法或类中供所有人使用。

As for testing an abstract test class, make sure you ask yourself what it is you're testing. 至于测试抽象测试类,请确保自己问自己测试的是什么。 There are several approaches, and you should find out what works in your scenario. 有几种方法,你应该找出适用于你的场景的方法。 Are you trying to test out a new method in your subclass? 您是否尝试在子类中测试新方法? Then have your tests only interact with that method. 然后让您的测试仅与该方法交互。 Are you testing the methods in your base class? 您是否正在测试基类中的方法? Then probably have a separate fixture only for that class, and test each method individually with as many tests as necessary. 然后可能只为该类设置一个单独的夹具,并根据需要使用尽可能多的测试单独测试每个方法。


#3楼

If the concrete methods invoke any of the abstract methods that strategy won't work, and you'd want to test each child class behavior separately. 如果具体方法调用策略不起作用的任何抽象方法,并且您希望分别测试每个子类行为。 Otherwise, extending it and stubbing the abstract methods as you've described should be fine, again provided the abstract class concrete methods are decoupled from child classes. 否则,扩展它并如你所描述的那样对抽象方法进行存根应该没问题,只要抽象类具体方法与子类分离即可。


#4楼

我想你可能想要测试抽象类的基本功能......但是你可能最好通过扩展类而不覆盖任何方法,并对抽象方法进行最小努力模拟。


#5楼

To make an unit test specifically on the abstract class, you should derive it for testing purpose, test base.method() results and intended behaviour when inheriting. 要在抽象类上专门进行单元测试,您应该为测试目的派生它,测试base.method()结果和继承时的预期行为。

You test a method by calling it so test an abstract class by implementing it... 您通过调用方法测试方法,因此通过实现它来测试抽象类...


#6楼

What I do for abstract classes and interfaces is the following: I write a test, that uses the object as it is concrete. 我对抽象类和接口所做的工作如下:我编写了一个测试,它使用了具体的对象。 But the variable of type X (X is the abstract class) is not set in the test. 但是在测试中没有设置X类型的变量(X是抽象类)。 This test-class is not added to the test-suite, but subclasses of it, that have a setup-method that set the variable to a concrete implementation of X. That way I don't duplicate the test-code. 这个测试类没有添加到测试套件中,而是它的子类,它有一个setup-method,用于将变量设置为X的具体实现。这样我就不会复制测试代码了。 The subclasses of the not used test can add more test-methods if needed. 如果需要,未使用的测试的子类可以添加更多测试方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章