# Getting started with unit testing in Julia

**URL:** https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216
**Category:** New to Julia
**Created:** [January 3, 2019, 12:02am UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216 "2019-01-03T00:02:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)
#### Post date: [January 3, 2019, 12:02am UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216/1 "2019-01-03T00:02:34Z")

</div>

I’m coming from a (mostly) Fortran/Python background.

Can someone give me a place to start for writing/running unit tests for my Julia code? A tutorial somewhere perhaps? And getting coverage testing and CI running? I’ve written a bit in Julia already but I’m ready to get serious and I’d like to maintain the good software engineering habits I tried to foster when I did a lot of python…

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [January 3, 2019, 12:57am UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216/2 "2019-01-03T00:57:35Z")

</div>

The standard library [`Test`](https://docs.julialang.org/en/v1/stdlib/Test/index.html) is great for testing things. It provides a `@test` macro which can be used for most things, and a few other macros for special cases. There’s also `@testset` to group your tests, though that’s not mandatory. A taste:

```julia
julia> using Test

julia> @test 1 == 1
Test Passed

julia> @test 1 == sum([1,2])
Test Failed at REPL[3]:1
  Expression: 1 == sum([1, 2])
   Evaluated: 1 == 3
ERROR: There was an error during testing

julia> @testset "some tests" begin
           # Simple tests which pass
           @test 1 == 1
           @test sum([1,2,3]) == 6
           
           # Numerical testing with `isapprox` (\approx ≈):
           @test sqrt(2) ≈ 1.414213562
           # The following works to supply tolerance keywords to ≈:
           @test sqrt(2) ≈ 1.4142135623730951 rtol=1e-16 atol=0

           # Tests which fail give nice error reporting
           a = [1,2,3]
           @test sum(a) == 7
           
           # Exceptional circumstances can be tested for
           @test_throws DivideError 1÷0
           
           # Tests which currently don't work can be documented as such
           @test_broken 1 == 2
           # Tests which are so broken they shouldn't be run can be skipped
           @test_skip just_broken
       end

some tests: Test Failed at REPL[10]:13
  Expression: sum(a) == 7
   Evaluated: 6 == 7
Stacktrace:
 [1] macro expansion at ./REPL[10]:13 [inlined]
 [2] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Test/src/Test.jl:1083 [inlined]
 [3] top-level scope at ./REPL[10]:3
Test Summary: | Pass Fail Broken Total
some tests | 5 1 2 8
ERROR: Some tests did not pass: 5 passed, 1 failed, 0 errored, 2 broken.

```

To use `Test` most effectively, you should put your code into a proper package structure which will allow you to use `Pkg.test` (or simply `test YourPkg` from the pkg REPL mode). I suggest using [PkgTemplates.jl](https://invenia.github.io/PkgTemplates.jl/stable/) to generate a standard package structure including your choice of CI config for Travis / Appveyor / GitLab (/ others?), automatic upload of coverage data to Coveralls or CodeCov and other package stuff like the package definition, license file etc.

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [January 3, 2019, 4:48am UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216/3 "2019-01-03T04:48:26Z")

</div>

I used PkgTemplates to create a package. It creates a file ~/src/foo/test/runtests.jl , which contains a test that 1 == 2, which of course fails. When you have some code to test, you put the test there instead of “@test 1 == 2”.

---

<div class="post-metadata">

### Author: ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)
#### Post date: [January 19, 2019, 9:09pm UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216/4 "2019-01-19T21:09:23Z")

</div>

Thanks for the input.

A 2 minute walkthrough to set up a package using PkgTemplates would be nice…

---

<div class="post-metadata">

### Author: ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)
#### Post date: [January 20, 2019, 8:08am UTC](https://discourse.julialang.org/t/getting-started-with-unit-testing-in-julia/19216/5 "2019-01-20T08:08:25Z")

</div>

> [@Gus\_Hart](#):
>
> A 2 minute walkthrough to set up a package using PkgTemplates would be nice…

This is open source. Now when you have figured that out, you can make the video / blog post and post the link here.
