Questions tagged [ex-unit]

ExUnit is a unit testing library that ships as part of the Elixir standard library.

ExUnit Docs

117 questions
4
votes
1 answer

Cleanup ExUnit test examples

I have the following tests in phoenix app: defmodule TattooBackend.Web.API.V1.PasswordControllerTest do use TattooBackend.Web.ConnCase, async: true use Bamboo.Test alias TattooBackend.Web.Endpoint alias TattooBackend.Accounts.Account …
Mateusz Urbański
  • 7,352
  • 15
  • 68
  • 133
4
votes
1 answer

ExUnit - Running DB setup code once, before all tests

Good morning, I have some particularly expensive database setup code in my Elixir project that inserts required data into the database. I currently have my tests working such that this data is inserted prior to any tests that need it via a @tag…
Brandon
  • 3,091
  • 2
  • 34
  • 64
4
votes
1 answer

How to implement an "assert_difference" for ExUnit

I'd like to test how a function changes something in the database. I'm struggling with an ExUnit equivalent of the following ActiveSupport::TestCase test case: test "creates a database record" do post = Post.create title: "See the difference" …
Carsten
  • 531
  • 4
  • 15
4
votes
1 answer

Test method delegation in Elixir

I've got two modules: defmodule Base.ModuleOne do def do_stuff(opts) do "some stuff with opts" end end defmodule Base.ModuleTwo do defdelegate do_stuff(opts), to: Base.ModuleOne end What is the proper way of testing delegation without…
kovpack
  • 4,905
  • 8
  • 38
  • 55
4
votes
1 answer

Is there any way to use shared examples in the ExUnit?

In RSpec it is possible to create a test and refer to it from multiple places using shared_examples. It simplifies developer's life a lot. Is there any way to use shared examples in ExUnit?
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
3
votes
1 answer

Stop a GenServer after each test

Background I have a set of tests that need a GenServer to be started before. As a rule of thumb, I understand it is a good practice to cleanup after each test, so I also want to stop the GenServer after each test. Problem The problem here is that I…
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
3
votes
2 answers

Check all test files are compilable

I have a case when I need to check that all files in my app's test suite are compilable. I'm trying to create a list of all files and then compile them, but it turns out I can't do it without starting ExUnit. find apps/*/test -name "*.exs" | xargs…
denis.peplin
  • 9,585
  • 3
  • 48
  • 55
3
votes
2 answers

How can I use Elixir's doctest to test a protocol implementation?

I want to be able to doctest the implementation of a protocol in Elixir. Here is some example code: defprotocol Size do def size(data) end defmodule Foo do defstruct [] defimpl Size do @doc """ ## Examples iex>…
jonleighton
  • 1,041
  • 8
  • 12
3
votes
1 answer

Force Hackney request timeout in test

I'm looking for a way to be able to reproduce a Hackney HTTP POST request timeout in one of my controller tests. I'm also using ExVCR, but that doesn't seem to allow you to force a request timeout either. I've tried setting the Hackney timeout to 1…
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
3
votes
0 answers

List failing Elixir tests at the end of a run

Whenever there are multiple failures, I find it difficult to scroll through the verbose output and find the failing tests. Is there a way to list all the failing tests at the end of a test suite run? Actual Finished in 12.9 seconds 2 doctests, 241…
Dennis
  • 56,821
  • 26
  • 143
  • 139
3
votes
1 answer

Isolating scenarios in Cabbage

I am automating acceptance tests defined in a specification written in Gherkin using Elixir. One way to do this is an ExUnit addon called Cabbage. Now ExUnit seems to provide a setup hook which runs before any single test and a setup_all hook, which…
aef
  • 4,498
  • 7
  • 26
  • 44
3
votes
3 answers

How to assert two maps in Phoenix controller testing with pattern matching

In my phoenix controller testing, I am doing something like this, describe "update/2" do setup [:create_user] test "Edits, and responds with the user if attributes are valid", %{conn: conn, user: user} do response = conn |>…
chandradot99
  • 3,616
  • 6
  • 27
  • 45
3
votes
1 answer

Is there a way to test an IO output using Doctest in Elixir?

I'm totally ok with writing a "normal" test capturing the IO for this. Would just like to know if it is possible to use Doctest. An example would be: defmodule CLI do @doc """ Politely says Hello. ## Examples iex> CLI.main([]) …
Ricardo Valeriano
  • 7,533
  • 1
  • 24
  • 34
3
votes
1 answer

Elixir/ExUnit: how to test functions with system calls most elegantly?

Situation Normally, unit tests like ExUnit should be self-contained with input, function call and desired output, so that the test can run on any system and always tests correctly regardless of environment. On the other side, if your application…
3
votes
1 answer

Testing recursive IO prompt in Elixir/Erlang

I have a function confirm, which reads IO input, and depending by input, if it's y (yes) or n (no), it returns true/false, otherwise it calls confirm again, until any expected y or n is entered. @spec confirm(binary) :: boolean def confirm(question)…
gintko
  • 697
  • 8
  • 16