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
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 unusedandvariable "result" does not exist and is being expanded to "result()"(CompileError) test/my_test.exs:15: undefined function expression/0
1
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone.
What strikes me is th...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
I have a super simple question about elixir - how would I take a file like this
foo
bar
baz
and output a new file that enumerates th...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Other popular topics
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New









