Hi all, I have a protocol that declares some required functions. I would like to develop a set of tests so that I can run them against 2 or more classes that conform to the protocol without having to re-write the tests for each implementation.
I attempted to create an XCTestCase base class hoping to inherit from it for each class conforming to the protocol. But it does not seem like it's possible to subclass XCTestCase in Swift?
For a more concrete example. Let's start with a protocol:
I would like to create an XCTestCase for A and B that can verify the behavior of methods defined.
The same problem holds if Arithmetic was a base class. Perhaps I wish to test and ensure that subclass overrides still behave correctly.
Currently I don't see a way around this other than to:
#2 is also not ideal as it makes the tests and test results more difficult to understand. In addition, it makes it difficult to re-use the tests... e.g. if the protocol is in a framework and I expect 3rd parties to be able to create conforming classes... Ideally they should also be able to test conformance without having to re-write all the tests.
Any suggestions?
I attempted to create an XCTestCase base class hoping to inherit from it for each class conforming to the protocol. But it does not seem like it's possible to subclass XCTestCase in Swift?
For a more concrete example. Let's start with a protocol:
Code Block protocol Arithmetic { func add(Int, Int) -> Int func multiply(Int, Int) -> Int /// a whole lot more }
Code Block class A: Arithmetic { // implement add(), multiply(), etc. } class B: Arithmetic { // implement add(), multiply(), etc. }
I would like to create an XCTestCase for A and B that can verify the behavior of methods defined.
The same problem holds if Arithmetic was a base class. Perhaps I wish to test and ensure that subclass overrides still behave correctly.
Currently I don't see a way around this other than to:
Copy & Paste the test cases for each conforming class (or subclass)
Have one "master" test that has an array of the protocol (i.e. [Arithematic]) and loop over it for every single test case.
#2 is also not ideal as it makes the tests and test results more difficult to understand. In addition, it makes it difficult to re-use the tests... e.g. if the protocol is in a framework and I expect 3rd parties to be able to create conforming classes... Ideally they should also be able to test conformance without having to re-write all the tests.
Any suggestions?