ExUnit is a unit testing library that ships as part of the Elixir standard library.
Questions tagged [ex-unit]
117 questions
19
votes
1 answer
Elixir assert_raise doesn't catch exceptions
I wrote this test case:
assert_raise ArgumentError, myFn(a,b)
but it does not evaluate in the way I'd expect. myFn raises an ArgumentError (do: raise ArgumentError), but it is not caught by assert_raise.
The example in the docs works just…

Filip Haglund
- 13,919
- 13
- 64
- 113
17
votes
2 answers
Elixir/ExUnit: passing context from testcase to teardown/cleanup method (on_exit) possible?
Problem
I want to test an Elixir module that interacts with the host system and has methods that have side effects. For this question and to keep it brief, assume it is the creation of several directories. Those directories should of course be…

user493184
- 201
- 2
- 7
14
votes
3 answers
How to use connection with session in phoenix?
I have an authentication plug and I want to test my controllers. The problem is that the line in this plug has
user_id = get_session(conn, :user_id)
And it's always nil when I'm using this method (I used dirty hack before, but I no longer want to…

Alex Antonov
- 14,134
- 7
- 65
- 142
12
votes
3 answers
How to run ExUnit tests within IEx
I'm trying to launch IEx.pry within a test. However I cannot get to run the tests within an iex session. Note that I'm not using mix.
ExUnit.start
defmodule Calc do
def add(a,b) do
a + b
end
end
defmodule TheTest do
use ExUnit.Case
…

Manuel M
- 469
- 4
- 13
10
votes
2 answers
ExUnit without assert/refute, relying solely on pattern matching?
I'm testing the return value of a function. Which of the two is the preferred way?
test "extra verbose, using assert" do
{:error, reason} = MyModule.my_fun
assert reason == :nope
end
test "using pattern matching only" do
{:error, :nope} =…

Carsten
- 531
- 4
- 15
7
votes
0 answers
setup/teardown dets tables between each test in elixir?
We are trying to run each test in a completely clean dets environment, and each test is responsible for its own setup. We're running into issues where we can not completely remove the directory between tests.
How can we reset dets? Did we…

Tom Clearingbear
- 97
- 10
7
votes
3 answers
How to clear a mailbox in channelcase phoenix framework test
I am doing a channel test that is receiving lots of messages. I may receive a message during setup, adjust some state and then I want to assert ( or refute ) another copy of that message was sent. I think I could do this by clearing the mailbox…

crododile
- 145
- 12
7
votes
2 answers
How do I still print logs to console while running ExUnit tests?
For debugging purposes during a failing integration test, I would like to still be able to see my application logs. Is there a command I can pass to the mix test task to accomplish this?

o_o_o--
- 975
- 9
- 20
7
votes
1 answer
Use underscore (_) in ExUnit tests
I have a method in my elixir app, let's say Some.Module.func/1, that returns a tuple of two numbers. I'm writing tests in ExUnit and only need to test the first element in tuple and don't really care about the second one.
So far, I've tried doing…

Sheharyar
- 73,588
- 21
- 168
- 215
6
votes
1 answer
In Elixir, placing test files alongside their related modules
Is it possible in elixir to have my test modules in the same directory as the modules they are testing?
A typical Elixir project has a structure like this:
project
|-- lib
| `-- my_module.ex
`-- test
|-- my_module_test.exs
`--…

harryg
- 23,311
- 45
- 125
- 198
6
votes
3 answers
Abort ExUnit on the first test that does not pass
In Ruby, specifically RSpec, you can tell the test runner to abort on the first test that does not pass by the command-line flag --fail-fast. This helps a lot to not waste time or lose focus when fixing a lot of test in a row, for example when doing…

aef
- 4,498
- 7
- 26
- 44
6
votes
1 answer
How can I list all tests using `mix test`
Is there a command-line invocation I can used in conjunction with mix that will output all test names without running the tests?

Michael Bishop
- 4,240
- 3
- 37
- 41
6
votes
2 answers
How do I fake IO input when testing with ExUnit?
I have an Elixir program I'd like to test which gets input from the user via IO.gets several times. How would I go about faking this input in a test?
Note: I would like to return a different value for each IO.gets

al_
- 93
- 5
5
votes
1 answer
How to test models with required associations
Using Ecto 2.0:
defmodule PlexServer.BoardInstanceTest do
use PlexServer.ModelCase
alias PlexServer.BoardInstance
@valid_attrs %{board_pieces: [%PlexServer.BoardTileInstance{x: 0, y: 0}], empire: %PlexServer.EmpireInstance{}}
…

Alejandro Huerta
- 1,135
- 2
- 17
- 35
4
votes
0 answers
How do I validate an input in elixir?
I am trying to write tests using hound and elixir and I cant seem to figure out a way to validate what was input into an input field... There is obviously a way to do this, but I have not figured out how to do so....
Example:

Kody Berry
- 111
- 1
- 7