# Simple debugging: @testset, @debug, @assert

**URL:** <https://discourse.julialang.org/t/simple-debugging-testset-debug-assert/102346>\
**Category:** General Usage\
**Created:** [August 1, 2023, 11:08am UTC](https://discourse.julialang.org/t/simple-debugging-testset-debug-assert/102346 "2023-08-01T11:08:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [August 1, 2023, 11:08am UTC](https://discourse.julialang.org/t/simple-debugging-testset-debug-assert/102346/1 "2023-08-01T11:08:53Z")

</div>

I want to insert simple test code here and there into my small programs and am wondering what the best practice is. [What I write below likely includes some error due to my misunderstanding. I’m struggling to understand the `@test` and `@debug` capabilities.]

1. I sometimes want to carry out an “expensive” test:

```julia
@testset begin
  for i in 1:n
     somevalue = func_of(i)
    @test arr[i] == somevalue
  end
end

```

I want to disable this code in my “production” runs because this test consumes some significant CPU time. Reading the official documentation of `Test`, I haven’t been able to find the switch to disable the test code globally. I guess there is a global switch to disable all test code.

1. Debug print: I guess `@debug` is the way to go, but I have a few questions.

2.1: How to enable and disable the debug print globally? Again, the official documentation doesn’t include a simple example.

2.2: Is `@debug` totally independent of the `Test` package so that you need to control `@test` and `@debug` separately?

2.3: How to disable only the printout while still evaluating the expression? For example, this code

```julia
@debug readline(istream) # discard the line

```

doesn’t work. I always need to skip the line and I’d like to look at the skipped line when debugging my code, but `@debug` disable the function call itself when not in the debugging mode. I guess there is a simple way to do this:

```julia
@evaluate_and_print_when_debugging begin
   some calculation
   "debug message: the calculated value is $(someval)"
end

```

1. Is `@assert` redundant once you start to use `@test`? I’ve just started to explore `@test` and `@debug`. I’ve been using `@assert` for a long time but `@assert` doesn’t have a capability corresponding to `@testset`, so perhaps I should entirely migrate to the `Test` package . . . ?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [August 1, 2023, 11:25am UTC](https://discourse.julialang.org/t/simple-debugging-testset-debug-assert/102346/2 "2023-08-01T11:25:50Z")

</div>

Perhaps this is of interest for you [GitHub - MasonProtter/ToggleableAsserts.jl: Assertions that can be turned on or off with a switch, with no runtime penalty when they're off.](https://github.com/MasonProtter/ToggleableAsserts.jl)
