smahi
How write test for an Elixir script file (Mix.install)
Hi,
I would like to know how to write test for an Elixir script created using Mix.install, let say I have script.exs then what is the best way to write test for it?
Thank you in advance.
Marked As Solved
RudManusachi
not sure if this is the best way, but it should work =)
script.exs
defmodule Foo do
def bar, do: :baz
end
In the same dir put script_test.exs
Code.require_file("script.exs")
ExUnit.start()
defmodule FooTest do
use ExUnit.Case
test "bar/0 does :baz" do
assert :baz == Foo.bar()
end
end
and run the test with
elixir script_test.exs
5
Also Liked
smahi
@RudManusachi
It work like charm, thank you so much.
The following is a working example.
# demo.exs
Mix.install([
{:bunt, "~> 0.2.0"}
])
defmodule Demo do
def hello(name \\ "John") do
["Hello, ", :green, :bright, name]
|> Bunt.ANSI.format
|> IO.puts
end
end
Demo.hello("Smahi")
# demo_test.exs
Code.require_file("demo.exs")
ExUnit.start()
defmodule DemoTest do
use ExUnit.Case
import ExUnit.CaptureIO
test "hello/1" do
output = capture_io(fn -> Demo.hello("Jahn") end)
assert output == "Hello, \e[32m\e[1mJahn\e[0m\n"
end
end
7
RudManusachi
One important thing to keep in mind!
When we run Code.require_file/1 it compiles (executes) the file. So if we do some side effects, external service calls etc… those will be run each time we run tests 
Need to separate out the declaration and the usage.
5
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
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
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
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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 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
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New








