# Testing different constant definitions

**URL:** https://discourse.julialang.org/t/testing-different-constant-definitions/50376
**Category:** General Usage
**Tags:** testing, constants
**Created:** [November 18, 2020, 12:41pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376 "2020-11-18T12:41:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [November 18, 2020, 12:41pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/1 "2020-11-18T12:41:39Z")

</div>

I have a macro, which creates / defines useful constants for the user. I would like to test if the macro created the correct constants and if they function as expected. How can I get something like the following to work?

```julia
@testset "Set 1" begin
    const A = 1
    @test A == 1
end

@testset "Set 2" begin
    const A = 2
    @test A == 2
end

```

Edit: depending on the argument, the macro creates different constants.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 18, 2020, 12:59pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/2 "2020-11-18T12:59:26Z")

</div>

What is not working there?

Maybe if you share an example which does not work it will be easier to help.

Probably the redefining of the constant?

You might need to wrap the generation of the constants inside different modules.

```julia
julia> module T1
         const A = 1
       end

julia> module T2
         const A = 2
       end

julia> @test T1.A == 1
Test Passed

julia> @test T2.A == 2
Test Passed

```

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [November 18, 2020, 1:07pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/3 "2020-11-18T13:07:28Z")

</div>

Try the example. You get:

`ERROR: LoadError: syntax: unsupported `const` declaration on local variable around`

I thought about wrapping everything in modules. The only problem is that you can’t define modules within testsets. It’ll complain that the module isn’t being defined at toplevel. This means you’d have to define a bunch modules at the beginning and then test them separately, but this quickly becomes unwieldy.

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [November 18, 2020, 1:08pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/4 "2020-11-18T13:08:00Z")

</div>

May be I could use a macro that defines a module at top level, rather than locally within the testset. Let me try that.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 18, 2020, 1:54pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/5 "2020-11-18T13:54:52Z")

</div>

You cannot have `const` variables below global scope (i.e., inside function definitions, `for` loops, etc…). If your macro did this then it just generated invalid code, and there is not reason to test the macro, you should discard it.

If you will always call the macro always at top level, and you want to check if its behaviour is working in this situation, then you probably should do as you pointed out: “have to define a bunch modules at the beginning and then test them separately”, or you can call the macro for the global scope of all the tests, or yet you can put the tests inside the modules where you call the macro and in the testsets you just call these test functions.

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [November 18, 2020, 6:00pm UTC](https://discourse.julialang.org/t/testing-different-constant-definitions/50376/6 "2020-11-18T18:00:06Z")

</div>

So, I’ve fixed my problem by not using metaprogramming, when I should use more ordinary features of julia.
