# Multiple (Custom) Test Environments?

**URL:** <https://forum.elixirforum.com/t/multiple-custom-test-environments/51516>\
**Category:** Questions\
**Tags:** wallaby, phoenix, testing\
**Created:** [November 2, 2022, 7:09pm UTC](https://forum.elixirforum.com/t/multiple-custom-test-environments/51516 "2022-11-02T19:09:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingdomcoder](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kingdomcoder/32/24667_2.png) [@kingdomcoder](https://forum.elixirforum.com/u/kingdomcoder)\
**Post date:** [November 2, 2022, 7:09pm UTC](https://forum.elixirforum.com/t/multiple-custom-test-environments/51516/1 "2022-11-02T19:09:21Z")

</div>

I recently added Wallaby to a project.

To increase my test speed, I made a separate `MIX_ENV` hoping to run my browser tests in that environment while the unit tests run in the default `test` environment. _(Separating the tests is achieved using ExUnit’s tags)._

Running the browser tests in my custom environment fails with this error:

> \*\* (Mix) “mix test” is running in the “wallaby” environment. If you are running tests from within another command, you can either:
> 
> 1. set MIX\_ENV explicitly:  
> `MIX_ENV=test mix test.another`
> 2. set the :preferred\_cli\_env for a command inside “def project” in your mix.exs:  
> `preferred_cli_env: ["test.another": :test]`

I guess this means all tests must run in the same (`test`?) environment.

So, is it possible to separate my tests and get them to run in different environments?  
How can I achieve it (or why can’t I if it’s impossible)?

Thanks for any help you can offer.

---

<div class="post-metadata">

**Author:** ![mhanberg](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mhanberg/32/17162_2.png) [@mhanberg](https://forum.elixirforum.com/u/mhanberg)\
**Post date:** [November 2, 2022, 7:39pm UTC](https://forum.elixirforum.com/t/multiple-custom-test-environments/51516/2 "2022-11-02T19:39:51Z")

</div>

I wouldn’t change the MIX\_ENV for tests, I would do add an alias `mix test.features` which is aliased to `mix test --only feature`.

This would also require added the preferred\_cli\_env for that new command.

I’m not sure what there is to gain from changing the MIX\_ENV.

Do you simply which to have a command to run unit tests and a command to run wallaby tests?

---

<div class="post-metadata">

**Author:** ![kingdomcoder](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kingdomcoder/32/24667_2.png) [@kingdomcoder](https://forum.elixirforum.com/u/kingdomcoder)\
**Post date:** [November 3, 2022, 9:33am UTC](https://forum.elixirforum.com/t/multiple-custom-test-environments/51516/3 "2022-11-03T09:33:21Z")

</div>

Thank you @mhanberg

I used an alias, so I already have 2 separate commands for my test (`mix test` and `mix browser.test`).

Also, I didn’t set my `MIX_ENV` directly. I set my environment using `preferred_cli_env`

Initially, it was:

> `preferred_cli_env: ["browser.test": :test]`.

However, the problem with this was that even when I ran only the unit tests (`mix test`), the tests were still slow.

I observed that chromedriver still started even though none of the Wallaby tests were going to run.

So, I setup another environment (`wallaby`) exactly like the `test` environment so that I can achieve:

> {:wallaby, “~\> 0.30.0”, runtime: false, only: :wallaby}

and

> `preferred_cli_env: ["browser.test": :wallaby]`.

This was the setup that produced the error in the OP.

I hope the situation is clearer.

My goal is to have Wallaby removed from the `test` environment and then have a similar environment to run the wallaby tests in.

How can this be achieved?

---

<div class="post-metadata">

**Author:** ![mhanberg](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mhanberg/32/17162_2.png) [@mhanberg](https://forum.elixirforum.com/u/mhanberg)\
**Post date:** [November 20, 2022, 4:42pm UTC](https://forum.elixirforum.com/t/multiple-custom-test-environments/51516/4 "2022-11-20T16:42:23Z")

</div>

You can probably (with `runtime: false`) add to your test\_helper.exs

```elixir
if System.get_env("FEATURE_TESTS") == "true" do
  Application.ensure_all_started(:wallaby)
end

```

And then run your browser tests with `FEATURE_TESTS=true mix test --only feature` or maybe make an alias `"browser.test": [fn _ -> System.put_env("FEATURE_TESTS", "true") end, "test --only feature"]` (I think the anonymous function literal should work there but but it’ll need to be extracted)
