# Creating my first package/ How to test my simple function

**URL:** https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281
**Category:** New to Julia
**Tags:** question, package, function
**Created:** [April 9, 2022, 10:08pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281 "2022-04-09T22:08:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![o.eleftherakou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o.eleftherakou/32/35120_2.png) [@o.eleftherakou](https://discourse.julialang.org/u/o.eleftherakou)
#### Post date: [April 9, 2022, 10:08pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/1 "2022-04-09T22:08:20Z")

</div>

Hi!

I decided to create my very first package. This “baby” package contains only one function (I give an example):

```julia
using Plots
using Distributions
function easy(x::Vector{Float64})
a = print("The mean is:", mean(x)) 
b = print("\nThe variance is:", var(x))
c = plot(x)
end

```

The user gives

```julia
a=[1,5,4,7.6]
easy(a)

```

Then, the function prints the mean of a, the variance of a and a simple plot of a. I get some functions from the Distributions.jl package and the plot from the Plots.jl package. I have followed all the needed steps to create my package and it’s time to test it.

I found a simple function test example:

```julia
using MyPackage 
using Test
@testset "MyPackage" begin
x = 2
y = 2
@test MyPackage.sum(x,y) = = 4
end

```

This is true, so the test is passed. In this case, it’s logical to test this.

In my case, I didn’t create any mathematical or new complex structured functions to test; only a function that **prints** some statistical results and an output of a plot (this might help a student).

In my case, though, what do I need to test my function? Does it make any sense?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [April 9, 2022, 10:24pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/2 "2022-04-09T22:24:42Z")

</div>

I don’t know of a way to do this with the standard library, but there are two packages that can help by capturing the output of your function to a string which you can then test against an expected result:

[https://github.com/JuliaDocs/IOCapture.jl](https://github.com/JuliaDocs/IOCapture.jl)

[https://github.com/JuliaIO/Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl)

---

<div class="post-metadata">

### Author: ![o.eleftherakou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o.eleftherakou/32/35120_2.png) [@o.eleftherakou](https://discourse.julialang.org/u/o.eleftherakou)
#### Post date: [April 9, 2022, 10:51pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/3 "2022-04-09T22:51:14Z")

</div>

Thank you! But what do I test here? My easy() function or every print separately? The type of easy(a) is Plots.Plot{Plots.GRBackend} because the prints are only prints. See the screenshot below:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/4/a4fc2602bde470cd76ebeed7f7b152ed43a2cd7d.png)

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [April 9, 2022, 11:02pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/4 "2022-04-09T23:02:43Z")

</div>

It is probably worth looking at the tests for plots.jl [https://github.com/JuliaPlots/Plots.jl/tree/master/test](https://github.com/JuliaPlots/Plots.jl/tree/master/test) to see what you can query about a plot once it has been created. Then you can decide which of those attributes are important to your function and ensure they match the expected value.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 9, 2022, 11:09pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/5 "2022-04-09T23:09:45Z")

</div>

If you return the values in the function, can can test them. Here is an example:

```julia
function easy(x::Vector{Float64})
    μ = mean(x)
    σ = std(x)
    print("The mean is:", μ) 
    print("\nThe variance is:", σ)
    c = plot(x)
    return μ,σ,c
end

x = [1.0,2.0,3.0]

μ,σ,p = easy(x)

@test μ ≈ 2.0
@test σ ≈ 1.0
@test p[1][1][:y] == x

```

I’m not sure how to test the print statements and plot further.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 9, 2022, 11:16pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/6 "2022-04-09T23:16:12Z")

</div>

As contradict suggested, you can test the print statement like so:

```julia
using IOCapture

c = IOCapture.capture() do
    easy(x)
end

@test c.output == "The mean is:2.0\nThe variance is:1.0"

```

---

<div class="post-metadata">

### Author: ![o.eleftherakou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o.eleftherakou/32/35120_2.png) [@o.eleftherakou](https://discourse.julialang.org/u/o.eleftherakou)
#### Post date: [April 9, 2022, 11:29pm UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/8 "2022-04-09T23:29:11Z")

</div>

Thanks to both of you! It worked like a charm! 🤩

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 10, 2022, 5:05am UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/9 "2022-04-10T05:05:34Z")

</div>

Can’t you just use `redirect_stdout` for this?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 10, 2022, 5:26am UTC](https://discourse.julialang.org/t/creating-my-first-package-how-to-test-my-simple-function/79281/10 "2022-04-10T05:26:24Z")

</div>

Extra credit: Functions that print things to stdout typically define two methods like this

```julia
function myfun(io::IO, x)
    println(io, 3x)
end

myfun(x) = myfun(stdout, x)

```

Testing this function is easier, because we can use our own `IOBuffer` instead of stdout.
