# Unit tests for interfaces

**URL:** <https://discourse.julialang.org/t/unit-tests-for-interfaces/6502>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 17, 2017, 1:00pm UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502 "2017-10-17T13:00:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 1:00pm UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502/1 "2017-10-17T13:00:04Z")

</div>

Is there a convenient way to unit test support for [interfaces](https://docs.julialang.org/en/stable/manual/interfaces/) in `Base`? I am thinking of something like

```julia
test_interface_array(my_array_like_object)

```

that would test that the type is `<: AbstractArray`, query `size` and `indices`, do a bit of consistency checking with `eltype` etc.

Similarly for iterators and indexing. I thought I would check before writing one. Or not writing one 😃 I keep repeating code for this in my own package unit tests.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 17, 2017, 2:09pm UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502/2 "2017-10-17T14:09:26Z")

</div>

> **[GitHub - oxinabox/InterfaceTesting.jl: (Julia 0.5 only) Concept still good,...](https://github.com/oxinabox/InterfaceTesting.jl)**
>
> (Julia 0.5 only) Concept still good, but needs updating for julia 1.0 - GitHub - oxinabox/InterfaceTesting.jl: (Julia 0.5 only) Concept still good, but needs updating for julia 1.0

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2017, 11:29am UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502/3 "2017-10-22T11:29:36Z")

</div>

I had some time to look into it now, and at this stage [InterfaceTesting.jl](https://github.com/oxinabox/InterfaceTesting.jl) only checks that some methods exist, not what they actually do. The API functions actually take a type, not an object. So while it is a step in the right direction, its usefulness is limited.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 22, 2017, 12:42pm UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502/4 "2017-10-22T12:42:21Z")

</div>

Feel Free to raise issues,  
or submit PRs.

My main initial goal with InterfaceTesting was to stop errors where `iteratoreltype`was declared on a object, not a Type,  
Which is why I started from there.  
It tests the interface type contract, when you look for the analogy to Java/C# interfaces.  
Which basically means asserting that the right methods exist.

I can certainly see it being reasonable to include some sanity checks.  
Something which requires that a typical instance of the item being tested be passed in.  
and then checks certain invariants hold.  
Though working out what they are is nontrivial.

Some ideas:

- `eltype(first(x))<:eltype(x)` seems reasonable for all iterators. possibly even doing something like checking the first up to 100.
- `x[first(indices(x))] === first(x)`
- `x[last(indices(x))] === last(x)`
- `length(x) = *(size(x)....)`
- probably some checks around `collect`

Anyway, make some issues/PRs.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 22, 2017, 12:44pm UTC](https://discourse.julialang.org/t/unit-tests-for-interfaces/6502/5 "2017-10-22T12:44:23Z")

</div>

> [@oxinabox](#):
>
> Anyway, make some issues/PRs.

Will do! Thanks for starting the package.
