Access variale between testsets

How can I take result from first testset and use it in second testset?

global result = 0;

@time @testset verbose=true "Upload" begin
    println(result)

    result = 1
end

@time @testset verbose=true "Update" begin
    println(result)
end

This is a common mistake–putting global at global scope doesn’t do anything at all. That is, doing:

global result = 0

and

result = 0

at global scope do exactly the same thing.

Instead, you need to put global where you set result in some local scope to tell Julia to set the global variable instead of creating a new local variable:

result = 0

@testset "a" begin
  global result  # Now within this scope, `result` will refer to the global variable
  @test result == 0
  result = 1
end

@testset "b" begin
  @test result == 1
end

Note that you can avoid the global issue and improve performance by using a ref instead:

const result = Ref(0)
 
@testset "a" begin
  @test result[] == 0
  result[] = 1
end

@testset "b" begin
  @test result[] == 1
end
1 Like

Thank you, I’ll use second solution.