# Looping over macro call produce unexpected results

**URL:** https://discourse.julialang.org/t/looping-over-macro-call-produce-unexpected-results/94675
**Category:** General Usage
**Created:** [February 15, 2023, 10:12am UTC](https://discourse.julialang.org/t/looping-over-macro-call-produce-unexpected-results/94675 "2023-02-15T10:12:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![brunbjerg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brunbjerg/32/53205_2.png) [@brunbjerg](https://discourse.julialang.org/u/brunbjerg)
#### Post date: [February 15, 2023, 10:12am UTC](https://discourse.julialang.org/t/looping-over-macro-call-produce-unexpected-results/94675/1 "2023-02-15T10:12:49Z")

</div>

Hello community

I have found a macro here on Julia Discourse to help me with some testing. The macro looks as follows:

```julia
macro testmsg(ex, str)
    quote
        try 
            @test $ex
        catch e
            $str |> println
        end
    end
end

```

The macro lets us define a custom message if @test fails. So…

```julia
julia> k = 1
1

julia> @testmsg k in 1:3 "No"
Test Passed

```

And

```julia
julia> k = 4
4

julia> @testmsg k in 1:10 "No"
Test Failed at c:\Users\s174460\.julia\dev\Scheduling\test\runtests_solution.jl:263
  Expression: k in 1:3
   Evaluated: 4 in 1:3
No

```

But if I loop over it I get:

```julia
julia> for k in 1:4
           @testmsg k in 1:3 "No"
       end
Test Failed at c:\Users\s174460\.julia\dev\Scheduling\test\runtests_solution.jl:263
  Expression: k in 1:3
   Evaluated: 4 in 1:3
No
Test Failed at c:\Users\s174460\.julia\dev\Scheduling\test\runtests_solution.jl:263
  Expression: k in 1:3
   Evaluated: 4 in 1:3
No
Test Failed at c:\Users\s174460\.julia\dev\Scheduling\test\runtests_solution.jl:263
  Expression: k in 1:3
   Evaluated: 4 in 1:3
No
Test Failed at c:\Users\s174460\.julia\dev\Scheduling\test\runtests_solution.jl:263
  Expression: k in 1:3
   Evaluated: 4 in 1:3
No

```

Like the for loop does not really update the value of k before inserting it into the macro. Does any of you have an idea of how to make this work?

Kind regards

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [February 15, 2023, 10:32am UTC](https://discourse.julialang.org/t/looping-over-macro-call-produce-unexpected-results/94675/2 "2023-02-15T10:32:15Z")

</div>

Try

```julia
macro testmsg(ex, str)
    quote
        try 
            @test $(esc(ex))
        catch e
            $str |> println
        end
    end
end

```
