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!

  • hallettj@beehaw.org
    link
    fedilink
    arrow-up
    13
    ·
    11 months ago

    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@lemm.eeOP
      link
      fedilink
      arrow-up
      5
      ·
      11 months ago

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

  • asyncrosaurus@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    11 months ago

    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.

  • Alex@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    11 months ago

    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.

  • Aurenkin@sh.itjust.works
    link
    fedilink
    arrow-up
    1
    ·
    11 months ago

    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.

  • freagle@lemmygrad.ml
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    11 months ago

    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)

  • kakes@sh.itjust.works
    link
    fedilink
    arrow-up
    0
    arrow-down
    1
    ·
    11 months ago

    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.