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.
Jasmine doesn't actually support parallel execution. It runs one spec (or before/after) function at a time.
Currently (v2. x) Jasmine runs tests in the order they are defined.
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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With