如何對抽象類進行單元測試:使用存根擴展?

本文翻譯自: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. 如果需要,未使用的測試的子類可以添加更多測試方法。

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