How to run tests in Elixir IEx shell

Hey! Have you ever wondered about tests running inside the IEx shell? For a long time, I was convinced that it’s not really possible. And as it turns out - that’s not really straightforward. You won’t easily find information about that in the documentation.

Table of contents

    But! If you dig deep enough in elixir-lang repository at github, you might find a Mix.Tasks.Test module, which is responsible for the mix test task running.

    Fortunately, there is a very useful function, which we can use for our own purpose:

    def run(args) do

    The arguments it receives are just the same as when you run the mix test causally but written as string values List. e.g["test/path_to/some_test.exs:666", "--seed", "420"])

    is equivalent to

    $ mix test test/path_to/some_test.exs:666 --seed 420

    Just last one thing that is to remember - if you want to run tests inside IEx, you have to set your MIX_ENV to test for environment.

    $ MIX_ENV=test iex -S mix

    Let's put it together

    $ MIX_ENV=test iex -S mix
    Finished in 1.0 seconds
    4 tests, 0 failures
    Randomized with seed 69090

    Use case example

    Of course, somebody could ask: Hey, but why would I even do that? I'm pretty sure, that there are lots of use cases. My favorite is to combine it with break!/4.

    Let's check it out:

    $ MIX_ENV=test iex -S mix
    iex(1)> break! SomeModule, :function_used_in_test, 1
    Request to pry #PID<0.716.0> at SomeModule.function_used_in_test/1 (lib/some_module.ex:309)
    309:   def function_used_in_test(args)
    Allow? [Yn] Y
    pry(1)> args
    ["debugging", "like", "that", "is", "wonderful"]
    pry(2)> continue
    Finished in 1.0 seconds
    4 tests, 0 failures
    Randomized with seed 69090

    That's so cool!

    Download our ebook
    Oskar Legner
    Oskar Legner Elixir & React Developer

    Read more
    on #curiosum blog

    Phoenix LiveView Tutorial: Adding Phoenix PubSub and Pow Authentication to Messenger

    We've already bootstrapped our Phoenix LiveView-based Messenger app's database structure and a first LiveView page.

    This time, we're going to improve real-time communication between the app's users using Phoenix PubSub, and use the Pow library to add secure user authentication.

    As of November 2020, the latest Phoenix LiveView version is 0.14.8 - and the series has been updated to match it!

    5 top-tier companies that use Elixir

    Elixir is a pretty capable language - and it consistently ranks near the top of most loved and wanted languages rankings. It has a large following and some very persuasive preachers as well. But that would not be enough to make me like it – what I need as real proof of its strengths is real businesses that strive with Elixir.

    That’s what this list is all about – a bunch of stories from top companies that chose Elixir and never looked back. Let us show you how its power and versatility shows in practice.