14

I'm trying to enforce independence between protractor tests within a spec. To detect whether or not tests are depending on a state introduced by an previous test, I would like to run those tests in random order.

Is there a way to tell protractor the order of tests can be randomized?

I found a feature request for Jasmine at pivotaltracker

TomVW
  • 1,510
  • 13
  • 26
  • 1
    Unit testing should ensure that the start after testing is the same as the state prior to testing - initialize, test, assert, restore. I'd be more inclined to ensure your test are right than to randomize them. – Enigmativity Jun 08 '16 at 12:17
  • 3
    I want to make sure that when new tests are added, they are independent. Therefore introducing the random factor would help identifying tests that fail this requirement. – TomVW Jun 08 '16 at 12:41
  • Randomizing them won't ensure that they are independent. It would be like sending cars randomly through an intersection - you might get a crash and you might not - but the roads are definitely not independent. You're not proving anything. You really need to make sure you lay down roads that are completely separate from each other to ensure they are independent. – Enigmativity Jun 08 '16 at 12:56
  • This is also a solid approach for weeding out dependent tests in a suite that already exists. I'm with you here. – Sam Berry Oct 16 '16 at 01:04

2 Answers2

7

You could execute the specs in a random order by shuffling them at the end of the suite:

var shuffle = function (items) {
  var item, ii;      
  for(var i = 0; i < items.length; i++){
    ii = (Math.random() * items.length) | 0;
    item = items[i];
    items[i] = items[ii];
    items[ii] = item;
  }
}

describe('Suite', function() {

  it("should a", function () {
      console.log("execute a");
  });

  it("should b", function () {
      console.log("execute b");
  });

  it("should c", function () {
      console.log("execute c");
  });

  shuffle(this.children);    // shuffle the specs

});
Florent B.
  • 41,537
  • 7
  • 86
  • 101
5

As of 10/10/17, it's possible to set a setting in the protractor.conf.js JasmineNodeOpts to run specs in semi-random order when using Jasmine, no code needed.

In your protract.conf.js file add the following json block:

  jasmineNodeOpts?: {
    ...
    /**
     * If true, run specs in semi-random order
     */
    random?: boolean,
    ...
  };

Source

S.Huston
  • 254
  • 6
  • 18