# How to test a function in Julia

**URL:** <https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196>\
**Category:** New to Julia\
**Tags:** question, testjl-unittesting\
**Created:** [March 29, 2021, 10:56pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196 "2021-03-29T22:56:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 29, 2021, 10:56pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/1 "2021-03-29T22:56:54Z")

</div>

Hi.

I have familiarity with unit testing in Python but I couldn’t really figure out how to do it in Julia when it comes to functions.

```julia
function year_created(p, language::String)
	loc = findfirst(p[:,2] .== language)
	return p[loc,1]
end

```

Based on some guides I’ve seen, I tried to add `@test` at the beginning of the function but it threw a `Test.FallbackTestSetException("There was an error during testing")`. I know that the way I did was amateur. I just don’t know what the steps are to test an entire function in Julia.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 11:01pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/2 "2021-03-29T23:01:05Z")

</div>

```julia
julia> using Test
       @test year_created([1500 "Portuguese"],"Portuguese") == 1500
Test Passed

```

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 29, 2021, 11:02pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/3 "2021-03-29T23:02:13Z")

</div>

Sorry but shouldn’t it be `year_created(1500, "Portuguese")`?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 11:03pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/4 "2021-03-29T23:03:37Z")

</div>

From what I understand of your function, `p` is as array in which the first column has the years, and the second column has the language name.

---

<div class="post-metadata">

**Author:** ![oo92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oo92/32/22963_2.png) [@oo92](https://discourse.julialang.org/u/oo92)\
**Post date:** [March 29, 2021, 11:04pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/5 "2021-03-29T23:04:49Z")

</div>

So my question is this. Do you have to test the function like this for every single entry in the dataframe?

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [March 29, 2021, 11:17pm UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/6 "2021-03-29T23:17:42Z")

</div>

> [@oo92](#):
>
> Do you have to test the function like this for every single entry in the dataframe?

Once you’ve written a decent unit test, then you will have tested your function. The test furnished by @lmiq should indicate that the basic functionality is correct, but it depends on what you intend. Do you want special behavior if a language appears more than once in the data, or is in lower case or all caps? Do you expect certain errors if data are not properly formatted? Where you “have to” test all of the desired features, or some meaningful subset, is up to you. Complete coverage of all possibilities is not always practical.

Your question about “every single entry in the dataframe” suggests you might have doubts about the integrity of data. That is a different matter, and might merit separate validation of data, not necessarily within `year_created`. Usually unit testing is used to refer to code, and not data, and once your code works, you expect it to do so for any data. But if you want to completely validate data, then probably you do have to write something to test every entry.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 30, 2021, 12:29am UTC](https://discourse.julialang.org/t/how-to-test-a-function-in-julia/58196/7 "2021-03-30T00:29:27Z")

</div>

> [@oo92](#):
>
> the dataframe?

You can broadcast the function, but that is not directly related to the tests:

```julia
julia> data = [ 1500 "Portuguese"
                1200 "Italian" ] 
2×2 Matrix{Any}:
 1500 "Portuguese"
 1200 "Italian"

julia> year_created.(Ref(data),data[:,2]) # broadcasting (note the dot ".")
2-element Vector{Int64}:
 1500
 1200

julia> @test year_created.(Ref(data),data[:,2]) == [1500, 1200]
Test Passed

#or
julia> @test year_created.(Ref(data),data[:,2]) == data[:,1]
Test Passed

```

But how do you want to actually design the tests is up to you.
