Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jasmine Spec multiple times in one execution

I want to run my jasmine test cases multiple times in one execution. Is there any looping or any other method to execute particular specs in one execution.

like image 621
gaurav kumar Avatar asked Aug 21 '14 05:08

gaurav kumar


People also ask

Do Jasmine tests run in parallel?

Jasmine doesn't actually support parallel execution. It runs one spec (or before/after) function at a time.

Do Jasmine tests run in order?

Currently (v2. x) Jasmine runs tests in the order they are defined.

What is spec in Jasmine?

A spec declares a test case that belongs to a test suite. This is done by calling the Jasmine global function it() which takes two parameters, the title of the spec (which describes the logic we want to test) and a function that implements the actual test case. A spec may contain one or more expectations.


Video Answer


2 Answers

You can run specs in an ordinary for loop like this:

for(var i = 0; i < 1000; ++i) {
    it("random is in [0;1)", function () {
      var r = Math.random();
      expect(r >= 0 && r < 1).toBeTruthy();        
   });
}

If you want to run it with different parameters it's a little bit tricky. You have to bind the loop parameter to a closure scope, otherwise the test will be called with last value of the loop parameter all the time.

// WRONG: test called 10 times with i == 10
for(var i = 0; i < 10; ++i) {
    it("i ^ 0 is 1", function () {
      expect(Math.pow(i, 0)).toEqual(1);
   });
}

// CORRECT: called with 1, 2, 3, 4....
for(var i = 0; i < 10; ++i) {
  (function(num) {
    it("i ^ 0 is 1", function () {
      expect(Math.pow(num, 0)).toEqual(1);
   });
  })(i);
}
like image 162
hansmaad Avatar answered Sep 23 '22 02:09

hansmaad


I'm using Standalone Release of Jasmine 2.0 with customized SpecRunner.html on a web server.

AFAIK, there is no looping function on Jasmine 2.0.

If you want to run whole test multiple times in the iteration of development, you can customize SpecRunner to reload result page automatically. I had written an auto refresh function that checks test spec and target's 'Last-Modified' header.

If you want to run multiple times a case, simply put describe() or it() into loop.

for (var i = 0; i < 10; ++i){
    describe('test multiple times', function () {
        it('may be fine', function () {
            expect(Math.random() < 0.5).toBeTruthy();
        });
    });
}

But the result would be ugly.

like image 28
Yasuo Ohno Avatar answered Sep 21 '22 02:09

Yasuo Ohno