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 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
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
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hello, how can I check the Phoenix version ?
Thanks !
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









