Problem with data-driven test case generation

Hi,

I’m trying to generate test cases from a JSON file. However, when I try the quote/unquote mechanism, it compiles but doesn’t find tests.

test/input.json

{
    "[1,2,3]": [ 1, 2, 3 ],
    "true": true,
    "{\"s\":1}": { "s": 1 }
}

tests/my_test.exs (working, finding three tests)

This is based on the @ AndyL discussion on ExUnit Data-driven Tests. I would like to get rid of the @expression stuff.

defmodule MyTest do
  import MyTestHelper, only: [parses_to: 2]
  use ExUnit.Case, async: true

  with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
       {:ok, json} <- Poison.decode(body) do
    Enum.each(json, fn {expression, result} ->
      @expression expression
      @result result

      @expression |> parses_to(@result)
    end)
  end
end

tests/my_test.exs (not working, not finding any test)

defmodule MyTest do
  import MyTestHelper, only: [parses_to: 2]
  use ExUnit.Case, async: true

  with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
       {:ok, json} <- Poison.decode(body) do
    Enum.each(json, fn {expression, result} ->
      quote do
        unquote(expression) |> parses_to(unquote(result))
      end
    end)
  end
end

tests/test_helper.exs

ExUnit.start()

defmodule MyTestHelper do
  use ExUnit.Case

  defmacro parses_to(expression, expected) do
    quote do
      test "Expression \"#{unquote(expression)}\"" do
        assert unquote(expected) === unquote(expression) |> Poison.decode!()
      end
    end
  end
end
1 Like
defmodule MyTest do
  import MyTestHelper, only: [parses_to: 2]
  use ExUnit.Case, async: true

  with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
       {:ok, json} <- Poison.decode(body) do
    Enum.each(json, fn {expression, result} ->
      parses_to(expression, result)
    end)
  end
end

Should do.

Thanks @hauleth, unfortunately that doesn’t solve the problem. I uploaded the code to chgeuer/ex_problem_repro1 for easier testing. When I replace

@expression expression
@result result
@expression |> parses_to(@result)

with

parses_to(expression, result)

it says

  • variable "result" is unused and
  • variable "result" does not exist and is being expanded to "result()"
  • (CompileError) test/my_test.exs:15: undefined function expression/0
1 Like