Explicit vs DRY tests

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).

2 Likes