开发者

apply test suite to implementations of a interface

开发者 https://www.devze.com 2023-04-05 09:07 出处:网络
I have a interface for which there exists several implementations. I\'m setting up a test suite for the interface. Now I want to run all test classes from the suite for each of the concrete implemento

I have a interface for which there exists several implementations. I'm setting up a test suite for the interface. Now I want to run all test classes from the suite for each of the concrete implementors.

to clarify, I have a setup like

  • TestSpam.java
  • TestEgg.java
  • TestBacon.java

which are all testing diffrent aspects of my interface IBreakfast, and I'm going to have them organised in a suite called say TestBreakfast. And I want all the diffrent tests to be run for specific implementation specified somehow, preferably once and on/to the suite.

At the testcase level I think I could use parametrise to run for all implementations, but doesn't look like that extends to suites. And also when 开发者_如何学Gousing parameterise it looks like I would have to hardcode the implementations which feels horribly backwards.

Any idea on how to make this work?


In this situation, I've repeatedly settled into a pattern like the following:

  • Create an abstract class which tests the expected functionality for the interface.
    • This class should declare an abstract factory method which returns an instance of the interface.
    • All of your tests should use the instance returned by the factory method.
  • Create one or more classes (one for each implementation) which extend this abstract class.
    • Implement the factory method by returning the appropriate concrete class.
    • Use this concrete test class to also test implementation-specific functionality.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号