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

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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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 Postg...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement