chgeuer

chgeuer

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

Most Liked Responses

chgeuer

chgeuer

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

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement