pierrelegall

pierrelegall

Hello alchemists! :woman_scientist: :man_scientist:

Tests are supposed to be readable/maintainable as every code should be in a perfect world. And explicitness is a good thing for readability.

Beginning my “one kata per day”, I wrote FizzBuzz tests in two ways. I don’t really know which style I prefer: one is very explicit and less concise. The other one is more in a DRY style.

What’s your opinion about this? :face_with_monocle:

Version 1:

defmodule Kata.FizzBuzz.Test do
  use ExUnit.Case

  import Kata.FizzBuzz

  test "1, 2, 4, 7, 8 returns number as a string" do
    assert fizz_buzz(1) == "1"
    assert fizz_buzz(2) == "2"
    assert fizz_buzz(4) == "4"
    assert fizz_buzz(7) == "7"
    assert fizz_buzz(8) == "8"
  end

  test "3, 6, 9, 12 returns Fizz" do
    assert fizz_buzz(3) == "Fizz"
    assert fizz_buzz(6) == "Fizz"
    assert fizz_buzz(9) == "Fizz"
    assert fizz_buzz(12) == "Fizz"
  end

  test "5, 10, 20, 25 returns Buzz" do
    assert fizz_buzz(5) == "Buzz"
    assert fizz_buzz(10) == "Buzz"
    assert fizz_buzz(20) == "Buzz"
    assert fizz_buzz(25) == "Buzz"
  end

  test "15, 30, 45, 60 returns FizzBuzz" do
    assert fizz_buzz(15) == "FizzBuzz"
    assert fizz_buzz(30) == "FizzBuzz"
    assert fizz_buzz(45) == "FizzBuzz"
    assert fizz_buzz(60) == "FizzBuzz"
  end
end

Version 2:

defmodule Kata.FizzBuzz.Test do
  use ExUnit.Case

  import Kata.FizzBuzz

  test "1, 2, 4, 7, 8 returns number as a string" do
    [1, 2, 4, 7, 8]
    |> Enum.each(fn n -> assert fizz_buzz(n) == "#{n}" end)
  end

  test "3, 6, 9, 12 returns Fizz" do
    [3, 6, 9, 12]
    |> Enum.each(fn n -> assert fizz_buzz(n) == "Fizz" end)
  end

  test "5, 10, 20, 25 returns Buzz" do
    [5, 10, 20, 25]
    |> Enum.each(fn n -> assert fizz_buzz(n) == "Buzz" end)
  end

  test "15, 30, 45, 60 returns FizzBuzz" do
    [15, 30, 45, 60]
    |> Enum.each(fn n -> assert fizz_buzz(n) == "FizzBuzz" end)
  end
end

Showing Posts 13 to 4

jarmo

jarmo

I always prefer explicit tests. Yes, they are more tedious to write, but much easier to understand when they start to fail.

Also, if trying to keep tests as DRY as production code then at one point they might get so complex that they need their own tests.

dimitarvp

dimitarvp

Weird how nobody did tests to get best from both worlds:

for n <- [1, 2, 4, 7, 8] do
  n = Macro.escape(n)

  test "fizz_buzz(#{n}) returns the input number as a string" do
    n = unquote(n)
    assert fizz_buzz(n) == "#{n}"
  end
end

Basically have your code generate tests. And if one fails you’ll have an exact test title + assert diff showing exactly what’s wrong.

I do such tests any chance I get because it helps cover several potentially-gotcha values.


But if we’re going into that territory then I’ll side with @hauleth – when you need to prove properties of your code then property testing is what you want. And it’s not an overkill at all, you use it 4-5 times and you get used to it. I have never spent more than 20-30 minutes adding property tests once I learned stream_data (which admittedly took me a weekend of tinkering, though it was well worth it).

kanishka

kanishka

You can get around this by making a loop around the “test” instead of the “assert” and putting the test value into the test name, but I generally don’t bother with the loop because of reasons stated above.

hauleth

hauleth

For me stream_data is a way to go from day one. It is “must have” just like credo and ex_doc.

pierrelegall

pierrelegall OP

After all, I really consider it as a nice feature proposal for ExUnit :grimacing:

pierrelegall

pierrelegall OP

True! Maybe because it tests all the case from 1 to 100 (or more easily), which is maybe not the purpose of a readable (as a spec) unit test.

I think it’s a bit too much to have a dependency to stream_data for a trivial unit test, I will avoid to use it for now :thinking:

I will start to apply a combination of the version 1 and 3 :grin:

hauleth

hauleth

My approach, as your is IMHO too repeating the implementation:

defmodule Kata.FizzBuzzTest do
  use ExUnit.Case
  use ExUnitProperties

  property "multiples of 3 contains `Fizz`" do
    check all n <- integer() do
      result = fizz_buzz(n * 3)
      assert String.contains?(result, "Fizz")
    end
  end

  property "multiples of 5 contains `Buzz`" do
    check all n <- integer() do
      result = fizz_buzz(n * 5)
      assert String.contains?(result, "Buzz")
    end
  end

  property "returned value is a binary" do
    check all n <- integer() do
      assert is_binary(fizz_buzz(n))
    end
  end

  property "result for 'plain' values is stringified integer" do
    check all n <- integer(), rem(n, 3) * rem(n, 5) != 0 do
      assert fizz_buzz(n) == Integer.to_string(n)
    end
  end
end

There probably could be some additional properties like the above

pierrelegall

pierrelegall OP

ExUnit outputs me this in an error case:

  1) test &fizz_buzz/1: multiples of 3 and 5 returns Fizz (Kata.FizzBuzz.Test)
     test/kata/fizz_buzz_test.exs:27
     Assertion with == failed
     code:  assert fizz_buzz(n) == "FizzBuzz"
     left:  "FizzBug"
     right: "FizzBuzz"

It point the problem: we would liked to know the value of n in this case :face_with_peeking_eye:

I’m surprised ExUnit does not already print out the values of the variables used in the failing code.

kanishka

kanishka

I agree here. It’s nice having very explicit, clear failures and not having to guess which one caused a failure. The assert message may provide a clear failure in this case. I think one should default to minimum abstraction in your tests, if there are no other pressing concerns like complexity of function under test.

I usually look at the failure message from my tests to decide whether I like the style of the test.

tfwright

tfwright

I think I am an outlier when it comes to tests, but I would not include any logic to generate assertions in unit tests. I would just choose one type of input for each type of output, with a test for each. If this logic was extra critical or issues keep popping up with unexpected inputs, I guess I would consider adding some sort of property based test to supplement the unit tests, but I would consider carefully before doing it.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
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
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews