# How to debug-break in unit test

**URL:** https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717
**Category:** New to Julia
**Tags:** debugging
**Created:** [April 6, 2021, 9:26pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717 "2021-04-06T21:26:32Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)
#### Post date: [April 6, 2021, 9:26pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/1 "2021-04-06T21:26:32Z")

</div>

Can anyone suggest how to debug break in unit test. My unit tests use standard Test.jl with @testset, @test macro and I start them from VS Code as ‘] test MyPackage’.

Thank you very much,

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 7, 2021, 10:00am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/2 "2021-04-07T10:00:24Z")

</div>

It is hard to suggest a specific action without more details. `@test` just gets a boolean that should be true, so usually one tries to figure out why it isn’t. You should be able to investigate the broken test in isolation.

---

<div class="post-metadata">

### Author: ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)
#### Post date: [April 7, 2021, 2:31pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/3 "2021-04-07T14:31:02Z")

</div>

Nothing special. I have set of tests in test folder and use Test.jl, all standard. It is usually not needed to debug them, because they are small and, if fail, it is enough to review the code. But some tests are bigger, sort of end to end, I want to debug break at some function to see values of local variables, and step through.

I usually run tests from Julia REPL in VS Code: activate my project and then do ‘] test MyProject’. There is no option to start with debugger there. I wonder if it is possible to insert some macro at some function to @debug\_break, or something.

Or maybe you can advice another method, such as organize test so that they can be called from REPL as regular function (so it is easy to run them with debugger)?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 7, 2021, 3:19pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/4 "2021-04-07T15:19:44Z")

</div>

> [@asyrov](#):
>
> organize test so that they can be called from REPL as regular function

Tests are code like any other Julia code, so you can just run them.

---

<div class="post-metadata">

### Author: ![asyrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asyrov/32/13578_2.png) [@asyrov](https://discourse.julialang.org/u/asyrov)
#### Post date: [April 7, 2021, 3:53pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/5 "2021-04-07T15:53:13Z")

</div>

So, for example, if I have the following test, how do I run it under debugger then?

```julia
@testset "mytest" begin
           # some code goes here 
           @test a == b
end

```

(note that `some code` may depend on something included earlier, with regular code I can just ‘using MyPackage’ and then call any function under debugger with some parameters.)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 8, 2021, 12:01pm UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/6 "2021-04-08T12:01:41Z")

</div>

There is not much you can debug about `a == b`, you should debug the code that generates `a` and `b`. Eg maybe run `# some code`, in the debugger if you prefer.

---

<div class="post-metadata">

### Author: ![mscott99](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mscott99/32/37802_2.png) [@mscott99](https://discourse.julialang.org/u/mscott99)
#### Post date: [November 14, 2022, 6:41am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/7 "2022-11-14T06:41:26Z")

</div>

I have the same issue. Running “Test” from the pkg REPL works. I try to run the test file in debug mode in vscode and the way it imports the main package will not work.

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [November 14, 2022, 8:53am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/8 "2022-11-14T08:53:28Z")

</div>

Perhaps you could try `TestItemRunner.jl` ([Prerelease of new testing framework and test run UI in VS Code](https://discourse.julialang.org/t/prerelease-of-new-testing-framework-and-test-run-ui-in-vs-code/86355)). This will let you isolate your unit tests and integrate with VS code’s built in Test viewer. From there you can run individual tests. There is also usually an option to debug an individual test, but the framework is still in beta so I’m not 100% sure that feature is there. However, the runner lets you launch a specific unit test from the REPL and you can just use the standard `@enter` or `@run` (the former seems to be more reliable for me) to enter debugging mode.

---

<div class="post-metadata">

### Author: ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)
#### Post date: [November 14, 2022, 10:24am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/9 "2022-11-14T10:24:54Z")

</div>

> the way it imports the main package will not work

is a bit unspecific, but maybe [`TestEnv.jl`](https://github.com/JuliaTesting/TestEnv.jl) is what you’re looking for? It helps to activate the test environment and make the “extra” dependencies available (as they would be during `pkg> test`).

During debugging, I like to run my testes “interactive”, that is not using the pkg test environment but just open the test file as a script executing it line by line “notebook style” and observe the repl output. If something is not working, I tend to just add some `@show` and `@info` statements to the functions I am debugging and reevluate the lines in the test file until I find the bug. For me, this kind of interactive testing/programming is one of the nicest things about julia. Once you’re using the test files as a “scratch space” where you write and executed mini-tests to ensure functionality while implementing new stuff, it becomes really easy to transform them into unit tests later.

---

<div class="post-metadata">

### Author: ![mscott99](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mscott99/32/37802_2.png) [@mscott99](https://discourse.julialang.org/u/mscott99)
#### Post date: [November 16, 2022, 5:39am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/10 "2022-11-16T05:39:55Z")

</div>

I wasn’t aware of this package. I’ll try it out, it looks promising.

---

<div class="post-metadata">

### Author: ![mscott99](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mscott99/32/37802_2.png) [@mscott99](https://discourse.julialang.org/u/mscott99)
#### Post date: [November 16, 2022, 6:02am UTC](https://discourse.julialang.org/t/how-to-debug-break-in-unit-test/58717/11 "2022-11-16T06:02:24Z")

</div>

I am confused about how Julia knows to import the main package in “using PackageName”. It seems it works when running the import works even with debugger, I was too lazy checking, sorry.  
Thanks for suggesting that workflow, I think I’ll do that from now on.
