# Testing side effects Elixir

**URL:** <https://forum.elixirforum.com/t/testing-side-effects-elixir/23720>\
**Category:** Questions\
**Tags:** testing\
**Created:** [July 5, 2019, 11:37am UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720 "2019-07-05T11:37:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/32/11709_2.png) [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Post date:** [July 5, 2019, 11:37am UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/1 "2019-07-05T11:37:13Z")

</div>

## Background

I have a module that uses eralngs [`persistent_term`](http://erlang.org/doc/man/persistent_term.html). I have some tests that try to use this module and a test file that looks like the following:

```elixir
defmodule MyAppTest do
  use ExUnit.Case

  describe "group 1" do
    
    setup do
      :persistent_term.put("hello", :world)
      :ok
    end

    test "test 1" do
      assert MyApp.run(:random_data)
    end

  end

# describe group2 ....

end

```

## Problem

Here the code in the `setup` block will run before each test inside the `describe` block. However, since `persistent_term` is a side effect, this will leak to other tests. `MyApp.run/1` uses `:persistent_term`, so it means that `MyApp.run/1` has side effects.

## Question

How do you test functions with side effects in Elixir?  
How would you test `MyApp.run/1` without leaking the test data into other tests?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 5, 2019, 12:08pm UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/2 "2019-07-05T12:08:34Z")

</div>

```elixir
use ExUnit.case, async: false

```

This is probably the only answer for this…

But anyway, you are not testing the sideeffect, but you are testing your function under a certain environment. Slightly different…

Anyway, I still think that running them with `async: false` and properly cleaning up is your only option…

---

<div class="post-metadata">

**Author:** ![svarlet](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/svarlet/32/10907_2.png) [@svarlet](https://forum.elixirforum.com/u/svarlet)\
**Post date:** [July 5, 2019, 1:08pm UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/3 "2019-07-05T13:08:04Z")

</div>

Are you testing configuration instead of some code?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 5, 2019, 1:10pm UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/4 "2019-07-05T13:10:30Z")

</div>

It tests behaviour under a certain configuration if you want to say so…

And yes, sometimes I do so…

Anyway, under “testing sideeffects” I’d actually understand, to test if a file exists and has a given content after calling `File.write`.

---

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/32/11709_2.png) [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Post date:** [July 5, 2019, 5:04pm UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/5 "2019-07-05T17:04:16Z")

</div>

> [@NobbZ](#):
>
> Anyway, I still think that running them with `async: false` and properly cleaning up is your only option…

iirc, `async: false` does not mean my tests will run synchronously between each other, it means that each test file will run 1 at a time, instead of running all test files at the same time.

So it is unclear to me how this would be a solution to the issue at hand.

Am I incorrect?

> [@svarlet](#):
>
> Are you testing configuration instead of some code?

I’m testing code. The module at hand has functions that save data to :persistent\_term. I want to make sure that data is being saved correctly.

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [July 5, 2019, 5:24pm UTC](https://forum.elixirforum.com/t/testing-side-effects-elixir/23720/6 "2019-07-05T17:24:48Z")

</div>

👋

> [@Fl4m3Ph03n1x](#):
>
> tests will run synchronously between each other

Tests within a single test file always run after one another, never at the same time. You can update your setup block to clean the persistent term storage and then insert “hello”. This way all test block would start with known state.
