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.

Last Post!

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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
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...
3271 130286 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement