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?

First 6 of 6 Posts Switch mode

shanesveller

shanesveller

Consider using @tag and @moduletag syntax in your test modules together with --include and/or --exclude flags to the existing `mix test’ task. This could then be automated as new mix aliases. IIRC your goal is a canonical example of why those flags exist.

LostKobrakai

LostKobrakai

I’m wondering why you need :extra_applications here? Having the dependencies marked for only the :test env should be enough.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

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.

cpgo

cpgo

I dont know if it works but maybe you could try to create a base case with the moduletags for each test type

LostKobrakai

LostKobrakai

If you have two types of tests it’s enough to tag the one with the smaller numbers and either run everything except that one tag or nothing, but that tag.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

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.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement