# Test code with multi-threading

**URL:** <https://discourse.julialang.org/t/test-code-with-multi-threading/41754>\
**Category:** Performance\
**Created:** [June 19, 2020, 10:47pm UTC](https://discourse.julialang.org/t/test-code-with-multi-threading/41754 "2020-06-19T22:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [June 19, 2020, 10:47pm UTC](https://discourse.julialang.org/t/test-code-with-multi-threading/41754/1 "2020-06-19T22:47:32Z")

</div>

Hello,

I am writing a multi-threading of my code, but I would like to write unit tests to make sure I get the same results as in the serial case.

I am thinking of doing:

1. execute export JULIA\_NUM\_THREADS=6 in the bash
2. open julia and activate the environment of my package
3. run the tests

Is it the way to do?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 19, 2020, 10:52pm UTC](https://discourse.julialang.org/t/test-code-with-multi-threading/41754/2 "2020-06-19T22:52:20Z")

</div>

yes that should work,

also if you’re working with CI:

> [@Julia Multithreading (Threads.@threads) and Travis CI](https://discourse.julialang.org/t/julia-multithreading-threads-threads-and-travis-ci/16468):
>
> Hi all, in a number of packages I started experimenting with code that exploits Julia’s multithreading facilities, i.e. mainly the Threads.@threads macro. I am a bit confused as to how to enable automatic testing of my code using Travis CI, in particular, to enable multithreading on Travis, but also to test both with and without multithreading (i.e. once with Threads.nthreads() == 1 and once with Threads.nthreads()\>1). Is anybody aware of an example travis.yml configuration file that I could lo…

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [June 19, 2020, 10:57pm UTC](https://discourse.julialang.org/t/test-code-with-multi-threading/41754/3 "2020-06-19T22:57:04Z")

</div>

Yet another way:

```julia
PerformanceTestTools.@include_foreach(
    "tests_using_threads.jl",
    [nothing, ["JULIA_NUM_THREADS" => Threads.nthreads() > 1 ? "1" : "6"]],
)

```

ref [Home · PerformanceTestTools.jl](https://juliatesting.github.io/PerformanceTestTools.jl/dev/#PerformanceTestTools.@include_foreach)

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [June 19, 2020, 11:06pm UTC](https://discourse.julialang.org/t/test-code-with-multi-threading/41754/4 "2020-06-19T23:06:53Z")

</div>

Thank you for the references!
