What does it really mean to test an interface not the implementation?

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:

  1. IShape interface and test both implementations in a single test file?
  2. Write tests for Rectangle and Triangle class separately, testing their implementation of Area() ?
  3. 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!

abhibeckert,

Is that a real example, or a contrived one?

I don’t think it’s normal to write a test for IShape.Area() in your example. You’d only write tests for Rectangle.Area() and Triangle.Area().

Wether or not all of that is in the same file should, in my opinion, depend how many tests there are. If there are “too many” lines of code, split it into multiple files.

asyncrosaurus,

It’s a form of Black-Box Testing, essentially you want to validate expected behavior. Implementation can change, but your outcome should remain the same.

This is a big target for Test Driven Development, since your first step is to write the test with the expected outcome, then you write the most basic implementation, and when you can verify the behavior, then you are free to re-factor to improve implementation knowing your test will tell you if the behavior changes with each internal change.

fzz,
@fzz@programming.dev avatar

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.

pelya,

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.

Aurenkin,

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.

hoodlem,

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.

kakes,

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.

freagle, (edited )

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)

pizzahoe,

Thank you for answering. I understand this a lot better now.

hallettj,
@hallettj@beehaw.org avatar

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”.

pizzahoe,

Fuck! Now it makes sense to me. I thought it literally meant an interface. Thanks a lot for clarifying!

hallettj,
@hallettj@beehaw.org avatar

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines