# Soft scopes & globals in \`@testitem\`s

**URL:** <https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338>\
**Category:** General Usage\
**Tags:** testing, scope\
**Created:** [September 12, 2024, 4:24pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338 "2024-09-12T16:24:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [September 12, 2024, 4:24pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/1 "2024-09-12T16:24:09Z")

</div>

I just encountered the problem discussed in [Best solution to Julia's soft scope problem?](https://discourse.julialang.org/t/best-solution-to-julias-soft-scope-problem/70199) , but in the context of unit tests. Specifically, in code like this:

```julia
@testitem "foo" begin
    A = 0
    for i in 1:10
        A += i
    end
    @test A == 55
end

```

This succeeds when running tests via `pkg> test`, but fails when running in VS Code’s test pane. Presumably because they differ about whether they create a new scope for each test?

Should this be considered a bug in whatever the test pane is using behind the scenes (part of the [Julia Extension](https://www.julia-vscode.org/), I think?)? Or do test writers need to worry about scope problems like this, and sprinkle `let` and/or `global` declarations around? [I would advocate for not the latter.]

I have v1.120.2 of the Julia Extension installed.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2024, 5:59pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/2 "2024-09-12T17:59:29Z")

</div>

Do you mean `@testset` rather than `@testitem`?

The following works for me whether I’m in the REPL (soft scope) or running it as a script `julia foo.jl` (hard scope), so I don’t see how soft-scoping is relevant here:

```julia
using Test
@testset "foo" begin
    A = 0
    for i in 1:10
        A += i
    end
    @test A == 55
end

```

(The reason it works is that `@testset` wraps everything in a `let` block, which creates a new scope, so `A` is not a global variable.)

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [September 12, 2024, 7:25pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/3 "2024-09-12T19:25:26Z")

</div>

I do mean `@testitem`, as is used for `TestItemRunner` or `ReTestItems`; but more specifically in this post, the error happens in the VS Code testing pane, whose details I don’t understand very well (see this thread: [How does the Testing pane work?](https://discourse.julialang.org/t/how-does-the-testing-pane-work/115301)).

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [September 24, 2024, 7:44pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/4 "2024-09-24T19:44:55Z")

</div>

I wonder if anyone has further thoughts about this issue?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 24, 2024, 8:11pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/5 "2024-09-24T20:11:50Z")

</div>

> [@Ken\_Williams](#):
>
> I do mean `@testitem`, as is used for `TestItemRunner` or `ReTestItems`

If the scopes for those macros don’t work like `@testset`, I would report it as a bug in the package(s) that define the macro.

Writing macros is tricky!

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [September 24, 2024, 10:00pm UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/6 "2024-09-24T22:00:46Z")

</div>

I reported the scope difference in [Follow useful @testset behavior · Issue #51 · julia-vscode/TestItemRunner.jl · GitHub](https://github.com/julia-vscode/TestItemRunner.jl/issues/51), but it didn’t really get any support.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 25, 2024, 2:19am UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/7 "2024-09-25T02:19:11Z")

</div>

> [@aplavin](#):
>
> but it didn’t really get any support.

If they don’t want to implement it, it’s up to them, but you could also probably get the behavior you want with

```julia-auto
@testitem "foo" let
    # ...
end

```

instead of `begin`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [September 25, 2024, 11:07am UTC](https://discourse.julialang.org/t/soft-scopes-globals-in-testitem-s/119338/8 "2024-09-25T11:07:54Z")

</div>

> [@stevengj](#):
>
> `@testitem "foo" let`

Unfortunately, I don’t think this works at all.
