lok0613
Hello all,
I’m recently doing something similar to ExUnit, keeping a bunch of variables on “setup” block and then pass it to the next block which is “test” just like below.
defmodule MyTest do
use ExUnit.Case, async: true
setup do
{:ok, %{message: "hi"}}
end
test "my test title", %{message, msg} do
IO.puts(msg)
end
end
However, I didn’t managed to do this on my own.
defmodule XUnit do
defmacro __using__(_opts) do
quote do
import XUnit
@setups []
@tests []
@before_compile XUnit
def run() do
IO.puts("Running....")
run_setups()
run_displays()
end
end
end
defmacro setup(do: block) do
fn_name = String.to_atom("setup")
quote do
def unquote(fn_name)(), do: unquote(block)
@setups [unquote(fn_name) | @setups]
end
end
defmacro test(message, var, do: block) do
var = Macro.escape(var)
quote bind_quoted: [message: message, var: var, block: block] do
fn_name = String.to_atom("test " <> message)
def unquote(fn_name)(unquote(var)), do: unquote(block)
@tests [{fn_name, var} | @tests]
end
end
defmacro __before_compile__(_opts) do
quote do
def run_setups() do
@setups
|> Enum.each(fn setup_fn -> apply(__MODULE__, setup_fn, []) end)
end
def run_displays() do
@tests
|> Enum.each(fn {test_fn, params} ->
apply(__MODULE__, test_fn, [params])
end)
end
end
end
end
defmodule Test do
use XUnit
setup do
%{message: "hi"}
end
test "my test title", %{message: msg} do
IO.puts("print from test, #{msg}")
end
end
Test.run()
It turns out this error message.
warning: variable "msg" does not exist and is being expanded to "msg()", please use parentheses to remove the ambiguity or change the variable name
Main.exs:64: Test
** (CompileError) Main.exs:64: undefined function msg/0
(elixir) src/elixir_bitstring.erl:142: :elixir_bitstring.expand_expr/4
(elixir) src/elixir_bitstring.erl:27: :elixir_bitstring.expand/8
(elixir) src/elixir_bitstring.erl:20: :elixir_bitstring.expand/4
expanding macro: XUnit.test/3
I tried many ways but it still not really work as my expectation. This is my first question on elixir forum, so please could anyone give me some idea how to mimic the way that ex_unit was doing?
Thanks so much
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Hello and welcome,
You do have a syntax error,
It should be…
al2o3cr
Your
testmacro handles arguments differently than the one inExUnit:https://github.com/elixir-lang/elixir/blob/83ddb923d4294470e3c8d158a5486f89f75e1481/lib/ex_unit/lib/ex_unit/case.ex#L299
I suspect that’s the cause of your immediate error.
You’ll also need to figure out how the return values of
setupblocks make their way to the actual test functions; right now you’re passing the AST received bytest.Enum.eachis likely not the function you want here.lok0613
this is just an example…
lok0613
really… I put
Enum.eachbecause it would be multipletestmacros.al2o3cr
Example or not, unless your test cases have some kind of side-effect you won’t get any results from
Enum.eachbesides:ok.Kabie
There is a trick:
Kabie
Ahh, I was overthinking, this should works already.
But your problem is you didn’t pass results that setup returns to tests, something like:
lok0613
wow that works like a charm!
Thank you @Kabie, @al2o3cr for helping. I’m actually digging into the ExUnit source code see if I could find any clue but it’s quite complex tho.