smahi

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

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

Also Liked

smahi

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
RudManusachi

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 :slightly_smiling_face:
Need to separate out the declaration and the usage.

Where Next?

Popular in Questions Top

srinivasu
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
joaquinalcerro
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
itssasanka
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
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
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
ovidiubadita
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
kostonstyle
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
jc00ke
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
siddhant3030
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 Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
gshaw
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
joaquinalcerro
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
itssasanka
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
AstonJ
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
fireproofsocks
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
ashish173
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
fayddelight
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
vonH
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
joeerl
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

We're in Beta

About us Mission Statement