I’m using Ruby 1.9.3 with MiniTest. I know the best practice is to have one assertion per test, but there are cases wherein I want to dynamically make assertions, like when comparing arrays:
if added_skus.each { |s| assert(skus_in_cart.include?(s), "SKUs #{added_skus} or SKU amount do not match SKUs #{skus_in_cart} on Cart page.") }
puts "Product SKUs #{added_skus} match SKUs #{skus_in_cart} on Cart page."
end
As you know, when it hits the first assertion failure the test ends. I’d like to continue the test and report all failures after the test has finished.
I found a solution using an old version of test/unit and Ruby 1.8.7 (the add_failure method in the test/unit library), but I can’t find a good solution for MiniTest and Ruby 1.9.3. I would really appreciate some feedback (even if that means I’m doing it wrong – please point me in the right direction). Thanks!
Solution 1 – Combine assertions into a single assertion:
You could write your current code as a single assertion by considering all the skus together:
Solution 2 – Custom method for asserting multiple elements:
If you really want the assertion count to be per individual SKU (instead of the entire set), you could create a custom assertion method. Though I do not believe this adds any real benefit over Solution 1.
Example running code:
require ‘test/unit’