let’s say there is a Shape interface.
interface IShape { double Area(); }
A Rectangle class and a Triangle class implement it. Now should i write tests for:
- IShape interface and test both implementations in a single test file?
- Write tests for Rectangle and Triangle class separately, testing their implementation of Area() ?
- Do something else?
From what I see I am testing implementations either ways. How do you even test an interface without testing the implementation? Can someone please help clarify my doubts? Thanks!
That advice does not literally refer to
interface
the programming language feature. It means to test the observable behavior of a component, not internal implementation details.In your example, write tests for both Rectangle and Triangle that call
area
, and assert the result is correct. But do not test, for example, the order of mathematical operations that were run to calculate the result. The details of the math are an internal detail, not part of the “interface”.Fuck! Now it makes sense to me. I thought it literally meant an interface. Thanks a lot for clarifying!
No problem! I thought there was a good chance you already know the concept, just not in the exact, unfortunately-overloaded words of your post title.
Testing interface is not about exactly interface as language’s feature. It’s about testing outer public interface (public API) of you things, looking at it as a black-box and doesn’t matter what happens inside. That’s all it means.
From my understanding the interface being referred to in the original guideline doesn’t necessarily refer to an interface as in the language concept but the more general concept of the interfaces to a given part of the system. In your example, I would interpret it as suggesting that you should test the public interface of the shape ie, when I call the Area method with some known cases, does it return the expected value.
Testing the implementation details in this case could be something like testing that the Area method for a circle called the Math.Pi function which would be an implementation detail. I don’t know if it’s the best or most helpful example but that’s my interpretation at least.
It means do not test HOW it is done, only test WHY it is done. Obviously you cannot test an abstract interface, only an implemented one. However, you should not be testing HOW it is implemented. Instead, you should be testing that given X input you get Y output based on the expectations set in the interface.
For example, take method F(x, y) that is designed to take in an identifier x and use it to fetch you a record from some persistence object y. Testing the interface of F(x, y) would mean testing that given the input x you get what you expect from the persistence object y. Testing the implementation would mean testing that F(x, y) issues a call to a specific method of y. For example, if y has an interface with methods getRecordById, getAllRecords, and searchRecords, testing the implementation means asserting which of these methods gets called (usually with a test using a mock object that can be interrogated in this way). Testing the interface means not caring which of these methods are called when satisfying the request for F(x, y)
Thank you for answering. I understand this a lot better now.
There is no functionality in the interface so there is nothing to test. You test the logic of the implemented methods.
With that said, with reflection you can at least test that an interface has the right method names and annotations present, but I’ve extremely rarely found it useful to do so.
Not an “expert” in testing per se, but I’m not sure I would ever attempt to “test” an interface. As you said, there is no implementation. You can only test implementation, so #2 in your list.
Testing the interface is more like validating the architecture of your module.
Say, you have your interface IShape, which represents a geometric shape.
Now you decide to add some shape editing capabilities to your qpplication.
Can you rotate your IShape? Can you flip it? So your IShape interface needs methods rotate(double angle) and flip(enum direction). So you add a test that checks that your module exports methods rotate(double angle) and flip(enum direction).
Does your application requires rotating shapes by a fractional angle? Maybe rotating by 90⁰ is enough? So you replace rotate(double angle) with rotate90degrees(bool clockwise) in your test and you can simplify your implementation.