在一个进程中多次运行ExUnit测试时,如何避免"警告:重新定义模块Foo"

lpi*_*pil 2 elixir-mix elixir

我希望能够在正在运行的进程中多次运行ExUnit测试,例如iex.

为此,我的代码看起来有点像这样:

def test do
  # Unload test files
  ["test"]
  |> Mix.Utils.extract_files("*")
  |> Enum.map(&Path.expand/1)
  |> Code.unload_files

  # Reenable tasks
  ~w(loadpaths deps.loadpaths test)
  |> Enum.map(&Mix.Task.reenable/1)

  # Run the test suite
  Mix.Task.run("test", args)

  # Cleanup
  :elixir_config.put(:at_exit, [])
end
Run Code Online (Sandbox Code Playgroud)

这可以,但打印test/my_app/foo_test.exs:1 warning: redefining module FooTest我的测试文件中定义的每个模块.

我认为,因为我从:elixir_code_server这些警告中卸载这些文件不会被提出,但事实并非如此.

如果不诉诸沉默stderr等方法,我怎么能沉默或避免这些警告呢?

似乎有一个编译器标志可以用来抑制这些警告,但是没有明确的公共API来设置这个标志.我们似乎可以禁用这些警告消息,没有明确的API可以这样做.

请参阅elixir_compiler:get_opt/1
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_compiler.erl#L8-L13

查看elixir_module:check_module_availability/3它检查的位置elixir_compiler:get_opt(ignore_module_conflict)
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_module.erl#L408-L418

小智 7

经过一些尝试和错误的冲击,这是适合我的解决方案:

Code.compiler_options(ignore_module_conflict: true)
Run Code Online (Sandbox Code Playgroud)

干杯!

  • 这是正确的答案,尽管当您需要暂时将其关闭时应设置为`ignore_module_conflict:true`,而当您希望将其重新打开时应设置为'false'。 (2认同)

Ono*_*cci -1

这可能会做到这一点:

mix test --no-compile
Run Code Online (Sandbox Code Playgroud)

或者在您的情况下,将 args 设置为包含,--no-compile如下所示:

 # Run the test suite
 Mix.Task.run("test","--no-compile")
Run Code Online (Sandbox Code Playgroud)

注意:我现在无法测试该代码的正确性,因此如果它是错误的,请提前道歉。