Fl4m3Ph03n1x

Fl4m3Ph03n1x

Separate mix commands for unit and integration tests

Background

I have a project where I have separated my tests into two categories: Unit tests and Integration tests.

My objective is to have 3 commands that do the following:

  • mix test.unit: executes all unit tests
  • mix test.integration: executes all integration tests
  • mix test: executes mix test.unit and then mix test.integration

I can somewhat replicate my first objective, instead of mix test.unit I am using environment variables and I am running MIX_ENV=unit mix test.

Problem

There are however a few problems with this approach:

  • It is way more verbose than the intended command
  • The only way I know of taking advantage of this is by littering my mix.exs with environment variables and pattern matching function that do something depending on the environment
  • It is very easy to miss the environment variable name only to see things blow up
  • I dont get help not auto complete if I type mix help

Research

I thought about using a mix alias like in this post:

But not only am I unsure on how to do it, I don’t even think I can change the default behavior of mix test to run both unit and integration tests separately.

Code

Thus far, all I have no is a half-broken mix.exs file that works:

defmodule MarketManager.MixProject do
  use Mix.Project

  @test_envs [:unit, :integration]

  ##########
  # Public #
  ##########

  def project do
    [
      app: :market_manager,
      version: "0.1.0",
      elixir: "~> 1.9",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      elixirc_paths: elixirc_paths(Mix.env),
      test_paths: test_paths(Mix.env)
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: applications(Mix.env),
      mod: {MarketManager.Application, [env: Mix.env]}
    ]
  end

  ###########
  # Private #
  ###########

  defp applications(:integration), do: applications(:default) ++ [:cowboy, :plug]
  defp applications(_),     do: [:logger]

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:httpoison, "~> 1.6"},
      {:jason, "~> 1.2"},

      # Testing and Dev
      {:hammox, "~> 0.2", only: @test_envs},
      {:mix_test_watch, "~> 1.0", only: @test_envs, runtime: false},
      {:plug_cowboy, "~> 2.0", only: @test_envs}
    ]
  end

  defp elixirc_paths(env) when env in @test_envs, do: ["test/support", "lib"]
  defp elixirc_paths(_),     do: ["lib"]

  defp test_paths(:integration), do: ["test/integration"]
  defp test_paths(:unit), do: ["test/unit"]
  defp test_paths(_), do: ["test/unit"]

end

Which was inspired in the blog:

Questions

As you can probably tell from reading my mix.exs file, it is a half broken mess where, in order to compile, I have to mix the definitions of MIX_ENV=unit mix test and mix test (check the test_paths) functions.

This brings all sorts of pain when developing.

How can I fix this file, so it has the desired behavior?

Marked As Solved

Fl4m3Ph03n1x

Fl4m3Ph03n1x

This is from a blog I saw:

https://medium.com/flatiron-labs/rolling-your-own-mock-server-for-testing-in-elixir-2cdb5ccdd1a0


# mix.exs
def application do
  [
    extra_applications: [:logger],
    mod: {GithubClient.Application, [env: Mix.env]},
    applications: applications(Mix.env)
  ]
end

I think the author made a little confusion because she uses both applications and extra_applications and one of them was discontinued iirc, but overall it has valuable information.

Also Liked

Fl4m3Ph03n1x

Fl4m3Ph03n1x

The issue I see with that approach is that I have to add an extra @moduletag to every module, while if I manage to do it my way, all I have to do is put the test files inside the “integration” and “unit” folders and it will be self explanatory.

This said, that is a valid approach. Depending on how much trouble the setup I am looking for takes, I may consider your approach.

Where Next?

Popular in Questions Top

New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement