# How to ensure no arguments are unintentionally mutated when testing a function in Julia

**URL:** https://discourse.julialang.org/t/how-to-ensure-no-arguments-are-unintentionally-mutated-when-testing-a-function-in-julia/55611
**Category:** General Usage
**Created:** [February 19, 2021, 11:30am UTC](https://discourse.julialang.org/t/how-to-ensure-no-arguments-are-unintentionally-mutated-when-testing-a-function-in-julia/55611 "2021-02-19T11:30:08Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [February 19, 2021, 11:30am UTC](https://discourse.julialang.org/t/how-to-ensure-no-arguments-are-unintentionally-mutated-when-testing-a-function-in-julia/55611/1 "2021-02-19T11:30:08Z")

</div>

When testing Julia functions I sometimes want to check that none of the arguments are unintentionally mutated. It’s a bit labour intensive at the moment: I write code to backup the arguments via `copy`, call the function, compare arguments with their backups.

Is there a macro that can do that for me? Something like `@checkargsnotmutated myfunction(arg1, arg2, arg3)`.

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1613734109202200?thread_ts=1613734109.202200&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![PGS62](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgs62/32/207030_2.png) [@PGS62](https://discourse.julialang.org/u/PGS62)
#### Post date: [February 19, 2021, 5:09pm UTC](https://discourse.julialang.org/t/how-to-ensure-no-arguments-are-unintentionally-mutated-when-testing-a-function-in-julia/55611/2 "2021-02-19T17:09:38Z")

</div>

Thanks to Benoît Richard over on Julia Slack for his answer, which worked nicely:

You don’t actually need a macro a function should be sufficient. Something like

```julia
function test_no_mutation(func, args...)
    backups = copy.(args)
    func(args...)
    for (arg, backup) in zip(args, backups)
        @test arg == backup
    end
end

```

Should do the trick.
