# \[ANN\] NBTesting.jl: unit testing for IJulia

**URL:** https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892
**Category:** Community
**Created:** [March 27, 2017, 12:57am UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892 "2017-03-27T00:57:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [March 27, 2017, 12:57am UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/1 "2017-03-27T00:57:35Z")

</div>

[NBTesting](https://github.com/cstjean/NBTesting.jl) is a simple utility for writing tests in your [IJulia](https://github.com/JuliaLang/IJulia.jl) notebooks, alongside other plots and computations. How it works:

1. [Add tests to your notebook](https://github.com/cstjean/NBTesting.jl/blob/master/test/Water_Analysis.ipynb) using `Base.Test`, or your favorite testing framework.
2. Use `NBTesting.nbtest("Water_Analysis.ipynb")` to run the notebook’s code and tests. It will create and execute a file called [NBTest\_Water\_Analysis.jl](https://github.com/cstjean/NBTesting.jl/blob/master/test/NBTest_Water_Analysis.jl).
3. (Optional) Track this .jl file with git if all tests are successful.

`nbtest` will mostly run the notebook code as is (similar to [NBInclude.jl](https://github.com/stevengj/NBInclude.jl)), but it provides [a few ways to control which code gets executed when](https://github.com/cstjean/NBTesting.jl/blob/master/test/Water_Analysis.ipynb), and a `verbose=...` option for printing the headers (on by default - see `?nbtest` for details). The code is wrapped inside a module called `NBTest_[Notebook name]`, to isolate it from the current environment, and to make it easier to inspect the state of the notebook variables if a test fails.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 27, 2017, 2:23pm UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/2 "2017-03-27T14:23:57Z")

</div>

> [@cstjean](#):
>
> Use `NBTesting.nbtest("Water_Analysis.ipynb")` to run the notebook’s code and tests. It will create and execute a file called `NBTest_Water_Analysis.jl`.

This seems odd to me — why would you have to create a file in order to execute the code?

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [March 27, 2017, 2:58pm UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/3 "2017-03-27T14:58:30Z")

</div>

> [@stevengj](#):
>
> why would you have to create a file in order to execute the code?

You don’t have to. The goal is to produce a git-friendly `.jl` file, to track the changes to the tests (eg. to make `git-bisect` convenient - just `include` the failing test file). Tracking Jupyter notebooks in git is painful.

You can use `nbtest(...; outfile_name="/tmp/dummy.jl")` if you don’t care about it.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 27, 2017, 3:25pm UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/4 "2017-03-27T15:25:24Z")

</div>

> [@cstjean](#):
>
> You don’t have to. The goal is to produce a git-friendly `.jl` file, to track the changes to the tests

So you expect people to commit generated code into the git repo? That doesn’t sound ideal either.

I track Jupyter notebooks in git all the time… as long as they don’t contain binary data (e.g. images), it isn’t too bad.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 27, 2017, 3:26pm UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/5 "2017-03-27T15:26:39Z")

</div>

And there are also tricks to get git to do a better job with notebooks, if needed: [https://gist.github.com/pbugnion/ea2797393033b54674af](https://gist.github.com/pbugnion/ea2797393033b54674af)

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [March 27, 2017, 3:39pm UTC](https://discourse.julialang.org/t/ann-nbtesting-jl-unit-testing-for-ijulia/2892/6 "2017-03-27T15:39:51Z")

</div>

> [@stevengj](#):
>
> So you expect people to commit generated code into the git repo?

Why not? I get why that’s usually a bad idea, but in this case, it’s just a cleaned up version of the `.ipynb`.

> [@](#):
>
> I track Jupyter notebooks in git all the time… as long as they don’t contain binary data (e.g. images), it isn’t too bad.

Or plots… I don’t use NBTesting for writing “dedicated testing notebooks” (NBInclude.jl would be fine for that), but rather to make sure that any change to my code didn’t affect the results in my analysis/production notebooks.

> [@stevengj](#):
>
> And there are also tricks to get git to do a better job with notebooks

That could work, too.
