# Run tests on src file change: Best practice

**URL:** https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173
**Category:** New to Julia
**Created:** [January 1, 2019, 10:11pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173 "2019-01-01T22:11:08Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [January 1, 2019, 10:11pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/1 "2019-01-01T22:11:09Z")

</div>

What is the best practice to have Julia run unit-tests automatically when the `src` code changes?

A. Using 3rd party packages.  
B. Using only `Base` or `Stdlib` packages.

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [January 1, 2019, 10:16pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/2 "2019-01-01T22:16:53Z")

</div>

I’ll endeavor to revise the selected solution to point to any better alternatives proposed…

Current best practice appears to be set-out in this [SO answer](https://stackoverflow.com/questions/52703637/julia-rerun-unittests-upon-changes-to-files/52876631#52876631) by _anowacki_:

1. Launch a separate shell and Julia REPL.
2. Activate your package `dev` or `test` environment.

```julia
$ cd ~/src/your-package
$ julia --project=.

```

A. **Using 3-rd-party Packages** (Blocked by [Watcher issue #2](https://github.com/rened/Watcher.jl/issues/2))  
[Add test related packages to `Project.toml`](https://julialang.github.io/Pkg.jl/v1/creating-packages/#Adding-tests-to-the-package-1)

```julia
[extras]
Test = "<uuid-here>"
Watcher = "<uuid-here>"

[targets]
test = ["Test", "Watcher"]

```

1. Cut-and-paste the following:

```julia
$ julia -e "using Watcher"

```

B. **Using only `Base` or `Stdlib`**

**NOTE:**  
_The following does not trigger test runs when you create or delete a file. Only when you add/remove content to a file. To delete a file: first remove all content (a test run will be triggered), then delete the empty file._

1. Cut-and-paste the following:

```julia
$ julia --project=.
julia> import Pkg; import FileWatching: watch_file

julia> @async while true
           event = watch_file("src")
           if event.changed
               try
                   Pkg.pkg"test"
               catch err
                   @warn("Error during testing:\n$err")
               end
           end
       end

```

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [January 2, 2019, 3:10am UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/3 "2019-01-02T03:10:16Z")

</div>

If you want to catch file creation or deletion, you probably need to do a `watch_folder` on the parent folder, since those types of changes modify the folder data structure, not the file data structure.

An alternative is to a use a tool like `entr` to watch the specified set of files you care about and then trigger your tests based on that. I haven’t used it before, so I’m not sure of its limitations, if any.

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [January 2, 2019, 9:03pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/4 "2019-01-02T21:03:30Z")

</div>

> [@jpsamaroo](#):
>
> If you want to catch file creation or deletion, you probably need to do a `watch_folder` on the parent folder,

Thanks for taking the time to make the suggestion.

I had tied that, unfortunately that was throwing this error when submitted in the REPL:

```julia
ERROR: type Pair has no field changed

```

Did `watch_folder` work for you?

With time I might dig in but right now a partial solution is as above.

---

<div class="post-metadata">

### Author: ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)
#### Post date: [January 14, 2019, 12:39am UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/5 "2019-01-14T00:39:02Z")

</div>

[Jive v0.1.5](https://github.com/wookay/Jive.jl#watch) supports watch folders.

```julia
~/.julia/dev/Jive/test $ cat runtests.jl
using Jive # runtests
runtests(@ __DIR__ )

```

```julia
~/.julia/dev/Jive/test $ julia --color=yes -q -i runtests.jl jive/s
1/3 jive/skip/skip-calls.jl
    Pass 2 (0.29 seconds)
2/3 jive/skip/skip-functions.jl
    Pass 4 (0.02 seconds)
3/3 jive/skip/skip.jl
    Pass 4 (0.01 seconds)
✅ All 10 tests have been completed. (0.61 seconds)
julia> watch(@ __DIR__ , sources=[normpath(@ __DIR__ ,"..","src")]) do path
           @info :changed path
           runtests(@ __DIR__ )
       end
watching folders ...
  - jive/skip
  - ../src

```

when saving any files in the watching folders, it automatically run tests.

```julia
julia> ┌ Info: changed
└ path = "jive/skip/skip.jl"
1/3 jive/skip/skip-calls.jl
    Pass 2 (0.00 seconds)
2/3 jive/skip/skip-functions.jl
    Pass 4 (0.01 seconds)
3/3 jive/skip/skip.jl
    Pass 4 (0.01 seconds)
✅ All 10 tests have been completed. (0.15 seconds)

```

to stop watching

```julia
julia> Jive.stop(watch)
stopped watching folders.

```

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [January 15, 2019, 10:22pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/6 "2019-01-15T22:22:40Z")

</div>

Thanks for `Jive.jl`: it has really changed the way I develop Julia code!

I’ve had a bit of trouble making `Jive.watch` work the way I want, though. In particular, when strictly following the documentation (or your post above), tests are correctly re-run for every source file change, but only changes to the test cases are correctly accounted for. As opposed to changes in the package source code itself, which don’t seem to propagate to the environment in which tests are re-run.

I’ve eventually made things work by adding `Revise` into the mix:

```julia
using Revise
using Jive

watch($dir; sources=[normpath(joinpath($dir, "..", "src"))]) do path
    @info "File changed" path
    revise()
    include("runtests.jl") # uses Jive.runtests() to run tests
end

```

Did I miss something? Or is this one of the ways Jive is meant to be used? In the latter case, I’d be happy to make a PR to document this if you think it useful.

---

<div class="post-metadata">

### Author: ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)
#### Post date: [January 16, 2019, 4:13am UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/7 "2019-01-16T04:13:43Z")

</div>

you’re right. I’ll update for it in Jive v0.1.6  
thank you very much 🙂

---

<div class="post-metadata">

### Author: ![siraj-samsudeen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siraj-samsudeen/32/48884_2.png) [@siraj-samsudeen](https://discourse.julialang.org/u/siraj-samsudeen)
#### Post date: [April 9, 2023, 9:50am UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/8 "2023-04-09T09:50:30Z")

</div>

Is there any update on Jive or any other tool that helps one to implement a TDD workflow to run tests on save? I have looked at Jive - being totally new to Julia, it looks a bit overwhelming…

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [April 9, 2023, 8:00pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/9 "2023-04-09T20:00:26Z")

</div>

I think nowadays `Revise.entr` provides enough features to re-run tests when sources change. Something like this could be a good starting point:

```julia
using Revise
using YourProject

# Re-run tests when something changes in the `test` directory
# or the sources of `YourProject`
entr(["test"], [YourProject]) do
    @info "Running tests"
    try
        include("test/runtests.jl") # or Pkg.test()
    catch e
        showerror(stderr, e)
        print("\n")
    end
end

```

---

<div class="post-metadata">

### Author: ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)
#### Post date: [April 20, 2024, 3:13pm UTC](https://discourse.julialang.org/t/run-tests-on-src-file-change-best-practice/19173/10 "2024-04-20T15:13:16Z")

</div>

I use the following script: [Script for watching and running tests or certain files (using Revise.jl to avoid full re-compilation). Useful if you're taking a test-driven development. Requries the following packages to installed in the global environment: - `ArgParse` - `Revise` - `TestEnv` · GitHub](https://gist.github.com/torfjelde/62c1281d5fc486d3a404e5de6cf285d4)

Found it to work extremely well in practice
