There are a few problems to work around here - that makes an option of working with mix.ex_unit combo the way you would like to work with it very unattractive.
0. You have to run in test env so MIX_ENV=test iex -S mix is your startup command.
1. Mix keeps track of the executed tests forever (until VM dies or you do a manual clear of that state). So starting iex -S mix and executing Mix.Task.run "test' will work the first time around but will assume the "test" task already ran on the second invocation. To clear that state you can do Mix.TasksServer.clear.
2. ExUnit also has state that is assumed to be initialized once (with all the test suites to run) and gets emptied out as you run the tests. Unfortunately, that initialization happens at compile time of the test see ExUnit.Case source
@doc false
defmacro __before_compile__(_) do
quote do
if @ex_unit_async do
ExUnit.Server.add_async_case(__MODULE__)
else
ExUnit.Server.add_sync_case(__MODULE__)
end
def __ex_unit__(:case) do
%ExUnit.TestCase{name: __MODULE__, tests: @ex_unit_tests}
end
end
end
This pretty much assumes you have to recompile test code to be included for a run or add the module on your own. So if you simply re-run the test nothing is tested. You have to do :code.delete(MyTest) and then run Kernel.ParallelCompiler.files(["test/my_test.exs"]). If all you need is to re-run your test, you can do so with ExUnit.Server.add_sync_case(MyTest) and then do Mix.Task.run "test" (given you cleared Mix.TasksServer)
So this is what did work in my simple elixir projects:
17:38:19 alex@alexmac iextestrun > MIX_ENV=test iex -S mix
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.2.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Mix.Task.run "test"
tests
.
Finished in 0.06 seconds (0.06s on load, 0.00s on tests)
1 test, 0 failures
Randomized with seed 855115
[#Function<2.98623896/1 in Mix.Tasks.Test.run/1>]
iex(2)> Mix.TasksServer.clear
:ok
iex(3)> ExUnit.Server.add_sync_case(MyTest)
:ok
iex(4)> Mix.Task.run "test"
tests
.
Finished in 0.00 seconds
1 test, 0 failures
Randomized with seed 542355
[#Function<2.98623896/1 in Mix.Tasks.Test.run/1>,
#Function<2.98623896/1 in Mix.Tasks.Test.run/1>]
At the danger of all this information being utterly useless, the simplest thing to do with ex unit tests is to stop and restart your VM :-)
Cheers,
Alex.