# Test.jl seems to have problems catching ArgParse.jl's exceptions

**URL:** <https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795>\
**Category:** General Usage\
**Tags:** question, package, testing, argument, testjl-unittesting\
**Created:** [August 23, 2025, 8:14pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795 "2025-08-23T20:14:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkalaczynski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkalaczynski/32/218358_2.png) [@pkalaczynski](https://discourse.julialang.org/u/pkalaczynski)\
**Post date:** [August 23, 2025, 8:14pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/1 "2025-08-23T20:14:14Z")

</div>

Hi,

I’ve been implementing unit tests for my project, and generally I’m very happy with Test.jl, however I stumbled across a weird problem when trying to do tests for argument parsing. I handle it using ArgParse.jl and when running locally, `@test_throws Exception parse_commandline()` works fine. However, when I run it through `Pkg.test()`, I suddenly get an Error about a required option not being specified (I have 1 arg set as required for my project). I admit I’m not even sure if this is a problem with Test.jl or with ArgParse.jl (or my implementation 🙃 ). Any suggestions would be highly welcome.

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [August 24, 2025, 10:48am UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/2 "2025-08-24T10:48:49Z")

</div>

Hi! Are you testing by calling `parse_args()` with no arguments? This might interact with the system’s `ARGS` variable. The `ArgParse` package defines the function `parse_args([args], settings)`. If you pass it a list of test arguments, I hope it behaves better. It shouldn’t at all interact with the test environment.

It might also be possible to copy the system `ARGS` to a saved variable and then write to the system `ARGS`, but that is explicitly forbidden by the runtime in other languages, like C, so I’m unsure about doing that in Julia.

You mentioned that the testing environment is different from running the program. Yes, `Pkg.Test` sets up a little environment and modifies the list of packages. If you wanted to understand that better, there is a package called `TestEnv.jl` that replicates this test environment setup in a place where you can read about it. It’s complicated.

Cheers,  
Drew

---

<div class="post-metadata">

**Author:** ![pkalaczynski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkalaczynski/32/218358_2.png) [@pkalaczynski](https://discourse.julialang.org/u/pkalaczynski)\
**Post date:** [August 24, 2025, 12:31pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/3 "2025-08-24T12:31:28Z")

</div>

Hi Drew, thanks for your feedback! My function looks like this:

```julia
function parse_commandline()

    s = ArgParseSettings()
    @add_arg_table! s begin
        "-i"
        help = "Input file path."
        arg_type = String
        required = true
        "-o"
        help = "Output file name."
        arg_type = String
        default = "output"
        required = false
        "--fmt"
        help = "Output file format (`--fmt hdf5` or `--fmt csv`)."
        arg_type = String
        default = "csv"
        required = false
        "--plot-fmt"
        help = "Output file format for plots (e.g. `--plot-fmt png` or `--plot-fmt pdf`)."
        arg_type = String
        default = "png"
        required = false
...
        "--no-plots"
        help = "Turn off saving the visualisation of the results to files."
        action = :store_true
    end
    return parse_args(s)
end # parse_commandline

```

So you suggest to have

```julia
return parse_args(ARGS, s)

```

? 🤔 I’ve just tried it but I get identical problem with

```julia
@test_throws Exception parse_commandline()

```

I can get around it by adding

```julia
append!(ARGS, ["-i", "../data/example_image.dat"])

```

at the end of my `runtests.jl`, but it does not really seem like the most elegant solution.

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [August 24, 2025, 2:11pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/4 "2025-08-24T14:11:34Z")

</div>

For testing, would it work to do:

```julia
function parse_commandline(default_args=ARGS)
  s = ArgParseSettings()
  @add_arg_table! s begin
    ...
  end
  return parse_args(default_args, s)
end

```

Then pass your test arguments into `parse_commandline(["--test-arg", "test_value"])`. The issue you’re running into is that test environments can pack other args into the ARGS variable, so it’s good to stay away from it in tests.

---

<div class="post-metadata">

**Author:** ![pkalaczynski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkalaczynski/32/218358_2.png) [@pkalaczynski](https://discourse.julialang.org/u/pkalaczynski)\
**Post date:** [August 25, 2025, 9:44am UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/5 "2025-08-25T09:44:38Z")

</div>

Well, when I test the argparsing itself, I just start by doing

```julia
empty!(ARGS)

```

which deals with potential unwanted arguments.

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [August 29, 2025, 6:18pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/6 "2025-08-29T18:18:21Z")

</div>

That will cause problems if you start to use newer features in Test. The Test framework will use ARGS to support multithreading, and I think I’ve heard it uses ARGS already in some capacity.

Emptying ARGS will also make impossible a common use case: People add flags to their test suite to mark particular tests as `--slow` or `--localonly` so that they can run longer tests only when necessary.

I always like to make life easy by making things testable. There’s an older paper that talks about the SOCK principle. Testable code is simple, observable, controllable, and knowable. In this case, maximizing controllability is very doable.

Whatever you do, glad you’re thinking about testing. Enjoy.

---

<div class="post-metadata">

**Author:** ![pkalaczynski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkalaczynski/32/218358_2.png) [@pkalaczynski](https://discourse.julialang.org/u/pkalaczynski)\
**Post date:** [September 4, 2025, 3:35pm UTC](https://discourse.julialang.org/t/test-jl-seems-to-have-problems-catching-argparse-jls-exceptions/131795/7 "2025-09-04T15:35:36Z")

</div>

I was not aware of plans to use ARGS in Test.jl, this is indeed a reason to pass it to the function like this, thanks for the info 🙂
