<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="179980" data-post-id="179980">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Fl4m3Ph03n1x" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Fl4m3Ph03n1x/120/11709_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Fl4m3Ph03n1x
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I know I am joining the party quite late, but I have seen a way in which you can actually execute an alias with a given ENV set, <strong>without</strong> having to manually type “MIX_ENV=blabla” before the command.</p>
<p>You can do it like they do in ecto, use a <code>test_with_env</code> function that does <em>a lot</em> of dark magic <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> :</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp aliases do
    [
      "test.all": ["test.unit", "test.integration"],
      "test.unit": &amp;run_unit_tests/1,
      "test.integration": &amp;run_integration_tests/1
    ]
  end

  def run_integration_tests(args), do: test_with_env("integration", args)
  def run_unit_tests(args), do: test_with_env("test", args)

  def test_with_env(env, args) do
    args = if IO.ANSI.enabled?(), do: ["--color" | args], else: ["--no-color" | args]
    IO.puts("==&gt; Running tests with `MIX_ENV=#{env}`")

    {_, res} =
      System.cmd("mix", ["test" | args],
        into: IO.binstream(:stdio, :line),
        env: [{"MIX_ENV", to_string(env)}]
      )

    if res &gt; 0 do
      System.at_exit(fn _ -&gt; exit({:shutdown, 1}) end)
    end
  end
</code></pre>
<p>You can read more about this in the blog:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://spin.atomicobject.com/elixir-test-multiple-environments/">
  <header class="source">
      <img src="https://spin.atomicobject.com/wp-content/themes/spin/images/favicon.ico" class="site-icon" alt="" width="" height="">

      <a href="https://spin.atomicobject.com/elixir-test-multiple-environments/" target="_blank" rel="noopener nofollow ugc" title="12:00PM - 22 October 2018">Atomic Spin – 22 Oct 18</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/379;"><img src="https://spin.atomicobject.com/wp-content/uploads/Elixir.png" class="thumbnail" alt="" width="690" height="379"></div>

<h3><a href="https://spin.atomicobject.com/elixir-test-multiple-environments/" target="_blank" rel="noopener nofollow ugc">Unit and Integration Testing In Elixir with Multiple Environments</a></h3>

  <p>A guide to implementing custom Mix environments in Elixir, which can help simplify writing unit and integration tests in a sane way.</p>

  <p>
    <span class="label1">Est. reading time: 5 minutes</span>
  </p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Basically, the hardcore part is the command:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">System.cmd("mix", ["test" | args],
        into: IO.binstream(:stdio, :line),
        env: [{"MIX_ENV", to_string(env)}]
      )
</code></pre>
<p>Which executes a command at system level and executes the given command with any variable you pass it, setting the ENV explicitly but without you having to care about it.</p>
<p>A clever use of aliases IMHO.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="179980" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-i-set-the-env-at-alias/16304/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-179980" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="179980"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="179989" data-post-id="179989">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What’s the reason for using this “dark magic” over the simple method shown earlier?</p>
<aside class="quote" data-post="4" data-topic="16304">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/how-do-i-set-the-env-at-alias/16304/4" rel="nofollow">How do I set the env at alias??</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    there’s also this in mix.exs: 
def project do
    [
      …,
      preferred_cli_env: [
        coveralls: :test,
        "coveralls.detail": :test,
        "coveralls.post": :test,
        "coveralls.html": :test,
        "test.reset": :test,
        "test.integration": :test
      ]
    ]
  end
  </blockquote>
</aside>

<p>Ecto uses such a complex method, because it tests multiple adapter setups, which are meant to work completely independant (postgres/mysql/mssql). If you’re not in a similar situation it can be much simpler.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="179989" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-i-set-the-env-at-alias/16304/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-179989" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="179989"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="179994" data-post-id="179994">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Fl4m3Ph03n1x" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Fl4m3Ph03n1x/120/11709_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Fl4m3Ph03n1x
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>What’s the reason for using this “dark magic” over the simple method shown earlier?</p>
</blockquote>
<p>The main reason was that your solution didn’t work for the OP, as he mentioned.<br>
I also encountered the same issue and your solution didn’t work for my case either.</p>
<p>The solution I presented works for all cases, although, as you correctly pointed out, at an increased cost of complexity, also known to us mortals as <em>dark magic</em> <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<p>I simply wanted to make it clear that you <em>don’t</em> have to explicitly type <code>MIX_ENV=blabla</code> before your mix command if you want to test with an environment other that :test.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="179994" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-do-i-set-the-env-at-alias/16304/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-179994" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="179994"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>