# @test macro

**URL:** <https://discourse.julialang.org/t/test-macro/65531>\
**Category:** General Usage\
**Created:** [July 29, 2021, 10:48pm UTC](https://discourse.julialang.org/t/test-macro/65531 "2021-07-29T22:48:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alois/32/22558_2.png) [@Alois](https://discourse.julialang.org/u/Alois)\
**Post date:** [July 29, 2021, 10:48pm UTC](https://discourse.julialang.org/t/test-macro/65531/1 "2021-07-29T22:48:58Z")

</div>

I understand that

```julia
@assert 2 == 3; println("never")

```

stops execution. But why does

```julia
@test 2 == 3; println("never")

```

abort execution, even with Error? I mean it is just a test which failed, but this should not result in a breaking error.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 30, 2021, 12:51pm UTC](https://discourse.julialang.org/t/test-macro/65531/2 "2021-07-30T12:51:19Z")

</div>

Does it actually throw an error or does it “just” print that there was an error? What’s the error message you observe? How do you run julia to observe this?

(I’ve moved the topic to “Usage”, since “Meta Discussion” is about discussion of this platform.)

---

<div class="post-metadata">

**Author:** ![Alois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alois/32/22558_2.png) [@Alois](https://discourse.julialang.org/u/Alois)\
**Post date:** [July 30, 2021, 12:56pm UTC](https://discourse.julialang.org/t/test-macro/65531/3 "2021-07-30T12:56:23Z")

</div>

The error message is:  
Test Failed at …  
Expression: 2 == 3  
Evaluated: 2 == 3  
ERROR: LoadError: There was an error during testing in expression starting at …

Unfortunately, Julia aborts execution and will never print “never”.

---

<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 30, 2021, 12:59pm UTC](https://discourse.julialang.org/t/test-macro/65531/4 "2021-07-30T12:59:51Z")

</div>

What do you want to do? `@test` is for unit testing, maybe it isn’t the right tool for what you’re trying to achieve

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 30, 2021, 1:07pm UTC](https://discourse.julialang.org/t/test-macro/65531/5 "2021-07-30T13:07:24Z")

</div>

`@test` is intended to be used for unit testing inside a `@testset`, but I’m not sure this erroring is intended behavior either. A cursory look through the callstack indicates that all test failures are treated as having thrown an error, which obviously isn’t true. Ironically, this change seems to have been introduced as part of making tests communicate their failures better…

[https://github.com/JuliaLang/julia/blob/795935fd3a2d97b2f948cfb82a18da48743b622d/stdlib/Test/src/Test.jl#L898](https://github.com/JuliaLang/julia/blob/795935fd3a2d97b2f948cfb82a18da48743b622d/stdlib/Test/src/Test.jl#L898)

From my point of view, either behavior is fine, but I can see why you’d think otherwise.

---

<div class="post-metadata">

**Author:** ![Alois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alois/32/22558_2.png) [@Alois](https://discourse.julialang.org/u/Alois)\
**Post date:** [July 30, 2021, 1:07pm UTC](https://discourse.julialang.org/t/test-macro/65531/6 "2021-07-30T13:07:42Z")

</div>

It is okay to see the error message, if the test fails. But execution should just continue with the following instructions, which could be further, more detailed tests.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 30, 2021, 1:08pm UTC](https://discourse.julialang.org/t/test-macro/65531/7 "2021-07-30T13:08:54Z")

</div>

From a library author POV, encouraging people to use `@testset` instead of single standalone `@test` statements seems desirable.

---

<div class="post-metadata">

**Author:** ![Alois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alois/32/22558_2.png) [@Alois](https://discourse.julialang.org/u/Alois)\
**Post date:** [July 30, 2021, 1:13pm UTC](https://discourse.julialang.org/t/test-macro/65531/8 "2021-07-30T13:13:08Z")

</div>

The macro @testset has the same behavior and the following example will break and never print “never”:

```julia
@testset begin
     @test 2 == 3; println("never")
end
println("never")

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 30, 2021, 1:29pm UTC](https://discourse.julialang.org/t/test-macro/65531/10 "2021-07-30T13:29:38Z")

</div>

There are a few distinct behaviors here that behave quite differently (and I think at least two are buggy):

1. Running a file

Contents:

```julia
using Test

@testset begin
    @test 2 == 3; println("never1")
end
println("never2")

```

run using `julia test_file.jl` produces this output for me:

```julia
test set: Test Failed at /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:4  
  Expression: 2 == 3                                                                         
   Evaluated: 2 == 3                                                                         
Stacktrace:                                                                                  
 [1] macro expansion                                                                         
   @ ~/julia/usr/share/julia/stdlib/v1.7/Test/src/Test.jl:445 [inlined]                      
 [2] macro expansion                                                                         
   @ /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:4 [inlined]            
 [3] macro expansion                                                                         
   @ ~/julia/usr/share/julia/stdlib/v1.7/Test/src/Test.jl:1282 [inlined]                     
 [4] top-level scope                                                                         
   @ /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:4                      
never1                                                                                       
Test Summary: | Fail Total                                                                  
test set | 1 1                                                                  
ERROR: LoadError: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.          
in expression starting at /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:3 

```

Note that `never1` _is_ printed, between the error stacktrace and the test summary, but `never2` is _not_ printed (which seems wrong, I agree!)

1. Running the same code interactively

This behaves the same as above

1. Running just `@test` from a file

File content

```julia
using Test

@test 2 == 3; println("never1")
println("never2")

```

Output obtained by `julia test_file.jl`

```julia
Test Failed at /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:3           
  Expression: 2 == 3                                                                        
   Evaluated: 2 == 3                                                                        
ERROR: LoadError: There was an error during testing                                         
in expression starting at /d/Documents/Projects/juliaProjects/Discourse/test_stuff/test.jl:3

```

Nothing is printed (which does seem wrong, agreed).

* * *

I do remember this working though, so seems like a regression that slipped through. I think opening an issue about this (and linking this discourse thread as well as PR [#37809](https://github.com/JuliaLang/julia/pull/37809) since I think that introduced this change) would be awesome. I think this is a problem with handling of the fallback testset (in the case of `@test`), but I don’t know what the issue is with `@testset`, that one seems weird to me.
