locopati

locopati

I have a test case with a setup and multiple tests. I want to run the exact same tests a couple times, changing a variable each time. In Rspec, this is trivial with let and shared_examples. ExUnit.Parameterized looks like it applies to individual test cases but not to the entire set of tests. What’s the right way to accomplish this in ExUnit?

I’m fairly new to Elixir for what that’s worth.

Showing Posts 1 to 4

NobbZ

NobbZ

The way I usually do this is like this:

%{1 => 2, 2 => 3}
|> Enum.each(fn {input, exp} ->
  test "#{input} increased by one is #{exp}" do
    assert unquote(exp) = 1 + unquote(input)
  end
end)

For some more complex input values, you might need to unquote(Macro.escape(…)), but you’ll figure this one out as soon as ecto barfs at you because its unable to unquote something.

axelson

axelson

Scenic Core Team

Depending on your inputs you might also want to just put these all in a single test

test "input increases by one" do
  %{1 => 2, 2 => 3}
  |> Enum.each(fn {input, exp} ->
    assert exp == 1 + input
  end)
end

Although you could also extend @NobbZ approach to all the tests in the file by including multiple test declarations inside the Enum.each

NobbZ

NobbZ

I do not like having many asserts in a single test.

Especially for such things. If you to a --trace or otherwise try to get a list of slow test, than very simple tests with a lot of different input might appear as slow tests, but in fact its not one slow test, but thousand fast ones.

But I think this is really a matter of personal taste.

Qqwy

Qqwy

TypeCheck Core Team

It might also make sense to assign them to a local variable first in those cases.

I agree about the amount of asserts per test: Another problem is that later asserts will not be tested against once the first one fails.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews