# Test macro that checks no exception was thrown?

**URL:** https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394
**Category:** General Usage
**Created:** [June 14, 2020, 6:03pm UTC](https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394 "2020-06-14T18:03:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [June 14, 2020, 6:03pm UTC](https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394/1 "2020-06-14T18:03:13Z")

</div>

I want to implement a test that just need to know the function did not throw any exception. There’s `@test_throws` but that’s the exact opposite of what I need. Is there anything like that?

I could work around by using regular `@test` with an expected result but I don’t really care about the calculation in this case.

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [June 14, 2020, 6:39pm UTC](https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394/2 "2020-06-14T18:39:33Z")

</div>

Option 1:

```julia
@test_nowarn foo()

```

Option 2:

```julia
@test foo() isa Any

```

---

<div class="post-metadata">

### Author: ![Jan-van-Waaij](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan-van-waaij/32/4578_2.png) [@Jan-van-Waaij](https://discourse.julialang.org/u/Jan-van-Waaij)
#### Post date: [June 14, 2021, 3:31pm UTC](https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394/3 "2021-06-14T15:31:04Z")

</div>

The following gives a fail instead of an error when an exception is thrown:

```nohighlight
@test try foo()
    true
catch
    false
end

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [August 11, 2023, 10:32pm UTC](https://discourse.julialang.org/t/test-macro-that-checks-no-exception-was-thrown/41394/4 "2023-08-11T22:32:48Z")

</div>

> [@dilumaluthge](#):
>
> `@test_nowarn foo()`

It’s not clear to me that `@test_nowarn` is really the correct idiom here. For example, `@test_nowarn sqrt(-1)` results in a test error rather than a test failure:

```julia
julia> @testset "hello" begin
           @test true
           @test_nowarn sqrt(-1)
       end
hello: Error During Test at REPL[74]:1
  Got exception outside of a @test
  DomainError with -1.0:
  sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
  Stacktrace: # Stacktrace omitted for clarity.
Test Summary: | Pass Error Total Time
hello | 1 1 2 0.1s
ERROR: Some tests did not pass: 1 passed, 0 failed, 1 errored, 0 broken.

```

Note that the second line of output says that there was an exception outside of a `@test`, and the last line says that 0 tests failed and 1 errored. It still alerts us that something went wrong, but philosophically it seems like what we want is a test failure (which is what the try-catch approach gives us).
