# Combining @eval and @testset macros

**URL:** <https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250>\
**Category:** General Usage\
**Tags:** testing, macros, metaprogramming\
**Created:** [July 8, 2021, 4:45am UTC](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250 "2021-07-08T04:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajhynes7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ajhynes7/32/24383_2.png) [@ajhynes7](https://discourse.julialang.org/u/ajhynes7)\
**Post date:** [July 8, 2021, 4:45am UTC](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250/1 "2021-07-08T04:45:23Z")

</div>

I’m trying to test two types that have similar properties. I want to perform the same set of tests on instances of the two types. If I was using Python, I would use the `@pytest.mark.parametrize` decorator to accomplish this.

I’ve read about using the `@eval` macro to generate code in Julia. The following code works for me:

```julia
for symbol in [:MyType1, :MyType2]
    @eval @testset "Test my two types" begin
        obj = $symbol(args)

        @test foo(obj)
        @test bar(obj)
    end
end

```

I’d like to put the symbol into the testset name so two different names are printed on the terminal during the test run, instead of the same name twice. But this fails:

```julia
for symbol in [:MyType1, :MyType2]
    @eval @testset String(symbol) begin
        obj = $symbol(args)

        @test foo(obj)
        @test bar(obj)
    end
end

```

```julia
ERROR: LoadError: LoadError: UndefVarError: symbol not defined

```

Is there any way I can insert the symbol into the testset name?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 8, 2021, 7:17am UTC](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250/2 "2021-07-08T07:17:28Z")

</div>

Welcome! `@testset` can also be applied on a `for` loop and you can interpolate in its name the iterator of the loop, so the standard way to do this is:

```julia
@testset "Test type $(T)" for T in (MyType1, MyType2)
    obj = T(args)

    @test foo(obj)
    @test bar(obj)
end

```

There is also a similar example in [`@testset` docstring](https://docs.julialang.org/en/v1/stdlib/Test/#Test.@testset)

---

<div class="post-metadata">

**Author:** ![ajhynes7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ajhynes7/32/24383_2.png) [@ajhynes7](https://discourse.julialang.org/u/ajhynes7)\
**Post date:** [July 8, 2021, 4:40pm UTC](https://discourse.julialang.org/t/combining-eval-and-testset-macros/64250/3 "2021-07-08T16:40:14Z")

</div>

Thanks @giordano this worked for me!
