How to start the :ex_unit app when compiling?

682 views
Skip to first unread message

lijie

unread,
Nov 1, 2013, 5:46:10 AM11/1/13
to elixir-l...@googlegroups.com
I'm writting tests using Elixir, wrote some helper modules in lib/ folder to simpilify test cases. These helper modules calls ExUnit.Case.assert, like this:

defmodule AccountHelper do
  use ExUnit.Case
  def expect_login_success(result) do
    assert(result.code == 0)
    ...
  end
end

When compiles, some errors occured:

$ mix compile
== Compilation error on file lib/account_helper.ex ==
** (RuntimeError) cannot use ExUnit.Case without starting ExUnit application, please call ExUnit.start() or explicitly start the :ex_unit app
    /Users/lijie/source/elixir/lib/ex_unit/lib/ex_unit/case.ex:35: ExUnit.Case."MACRO-__using__"/2
    lib/account_helper.ex:2: ExUnit.Case.__using__/1
    lib/account_helper.ex:2: AccountHelper (module)

 It is just compiling, why need to start :ex_unit app? and how to resolve it?


Best regards.

Eric Meadows-Jönsson

unread,
Nov 1, 2013, 5:56:01 AM11/1/13
to elixir-l...@googlegroups.com
You should write test helpers in your test directory so that they are not compiled. You dont want to have test code in your production code. It is common to put this kind of code in test/test_helpers.exs. This file is auto required when running `mix test`.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Eric Meadows-Jönsson

José Valim

unread,
Nov 1, 2013, 6:42:01 AM11/1/13
to elixir-l...@googlegroups.com
The way ExUnit works is that at the moment you call use ExUnit.Case, it invokes the ExUnit server to register that test case so it can be run later. You could work around that but I agree with Eric that you don't want to have test code in your production code.



José Valim
Skype: jv.ptec
Founder and Lead Developer


On Fri, Nov 1, 2013 at 10:46 AM, lijie <cpu...@gmail.com> wrote:

--

lijie

unread,
Nov 1, 2013, 6:50:27 AM11/1/13
to elixir-l...@googlegroups.com
Thanks.

This is an integration test project, and I also want to use ex_unit to do automated testing, run all tests every few minutes on online system, the test code is also production code for this project. I can put code to test/ directory, but I think that put into lib/ directory to let it be compiled will faster.

I modified code to 'import ExUnit.Assertions', it works.

José Valim

unread,
Nov 1, 2013, 6:53:36 AM11/1/13
to elixir-l...@googlegroups.com
I modified code to 'import ExUnit.Assertions', it works.

Keep in mind that when running tests you will need to explicitly tell ExUnit to run those modules for testing. Besides that, everything should work fine.



José Valim
Skype: jv.ptec
Founder and Lead Developer
 

lijie

unread,
Nov 1, 2013, 8:29:07 AM11/1/13
to elixir-l...@googlegroups.com
Thanks, these modules in lib/ directory are helper modules, only contains some assertions, all test cases be put into test/ directory.
Reply all
Reply to author
Forward
0 new messages