Allowing multiple AssertionErrors before failing a test Allowing multiple AssertionErrors before failing a test selenium selenium

Allowing multiple AssertionErrors before failing a test


I can offer two approaches.

The first one, mentioned by @Peter Lyons, relies on converting multiple assertions into a single assertion on multiple values. To keep the assertion error message useful, it's best to assert on the list of names:

var expected = ['Alice', 'Bob', 'John'];var found = expected.filter(function(name) {   return pageContent.indexOf(name) >= 0;}// assuming Node's require('assert')assert.deepEqual(found, expected);// Example error message:// AssertionError: ["Alice","Bob","John"] deepEqual ["Alice"]

The second approach uses "parameterised test". I'll assume you are using BDD-style for specifying test cases in my code.

describe('some page', function() {   for (var name in ['Alice', 'Bob', 'John'])     itContainsString(name);   function itContainsString(name) {      it('contains "' + name + '"', function() {        var pageContent = 'dummy page content';        assert.include(pageContent, 'Alice');      });   }}


var found = ['Alice', 'Bob', 'John'].map(function (name) {  return pageContent.indexOf(name) >= 0;});assert.include(found, true);

If I may opine that your desire for a wrapper library for fuzzy asserting sounds misguided. Your fuzzy rules and heuristics about what is a "soft" vs "hard" assertion failure seem like a much less sensible alternative than good old programming using the existing assertion paradigm. It's testing. It's supposed to be straightforward and easy to reason about.

Keep in mind you can always take logic such as the above and wrap it in a function called includesOne(pageContent, needles) so it is conveniently reusable across tests.