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
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 unusedand variable "result" does not exist and is being expanded to "result()"(CompileError) test/my_test.exs:15: undefined function expression/0
1
Popular in Questions
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
can someone please explain to me how Enum.reduce works with maps
New
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
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
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
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
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
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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
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
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
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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








