# How to compare two types of Julia for unit testing?

**URL:** <https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 24, 2017, 8:19am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853 "2017-03-24T08:19:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [March 24, 2017, 8:19am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/1 "2017-03-24T08:19:58Z")

</div>

e.g. I have my type

```julia
type Foo
  version::String
  id::String
  samplingRate::Int
  timeBuilt::String
  equations::Vector{Equation}
  exam::Vector{Exam}
end

```

---

<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:** [March 24, 2017, 8:28am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/2 "2017-03-24T08:28:09Z")

</div>

Please clarify: do you want to compare types (eg `typeof(something)`) or their values? In the latter case, you should probably define `Base.==` (or something equivalent if it generally does not make sense and you just need it for unit testing), look at

> **[GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash()...](https://github.com/JuliaServices/AutoHashEquals.jl)**
>
> A Julia macro to add == and hash() to composite types. - GitHub - JuliaServices/AutoHashEquals.jl: A Julia macro to add == and hash() to composite types.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [March 24, 2017, 9:01am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/3 "2017-03-24T09:01:20Z")

</div>

Their all values. Can you give a simple example @Tamas_Papp , please?

---

<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:** [March 24, 2017, 9:16am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/4 "2017-03-24T09:16:07Z")

</div>

I think that that package is well-documented, but

```julia
using AutoHashEquals

@auto_hash_equals immutable Foo
    a
    b
end

Foo(1,2) == Foo(1,2) # true

```

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [March 24, 2017, 9:54am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/5 "2017-03-24T09:54:44Z")

</div>

Thanks very much 🙂 now it’s clear and simple or all types must be with @auto\_hash\_equals (e.g. type equations, Exam …) What with this types?

---

<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:** [March 24, 2017, 12:52pm UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/6 "2017-03-24T12:52:31Z")

</div>

For types, you can use `is` (which can be typed as `≡`), but most of the time you don’t need to do this; when I unit test for types I end up using `Base.Test.@inferred` most of the time.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 25, 2017, 8:04am UTC](https://discourse.julialang.org/t/how-to-compare-two-types-of-julia-for-unit-testing/2853/7 "2017-03-25T08:04:47Z")

</div>

`is` is going/has gone away; in the future `===` is the canonical name for this function. However testing the type should be done with `::` type assertions, not with `typeof`.
