I use protractor + jasmine. I have a test as below:
describe('Suite', function() {
beforeEach(function() {
// do precondition
});
it('Test', function() {
// do test
});
});
If my precondition was failed my test will execute. But I would like to skip test in this case. How can I do that?
Related
I am aiming to skip a spec files once an it() inside it fails. And I would still like to see the error reported so I know where to focus on.
I have two test scripts that is inside the same folder first.cy.js and second.cy.js:
describe('test body', () => {
afterEach(() => {
if (cy.state('test').state == 'failed') {
Cypress.runner.stop()
}
})
it('should pass first test', function() {
expect(true).to.eq(true)
})
it('should pass second test', function() {
expect(true).to.eq(true)
})
it('should fail third test and skip this script', function() {
expect(true).to.eq(false)
})
it('should skip fourth test', function() {
expect(true).to.eq(true)
})
})
My problem is on result reporting, it does not report the failing part:
Is there anything I am missing here?
EDIT: I also need to mention that we have retries set on cypress config:
retries: {
runMode: 2
},
Update: Was able to figure out how I will skip the spec file and still record errors with the help of Alapan das:
describe('skipping with retrying and recording', () => {
let errorCount = 0
afterEach(() => {
if (cy.state('test').state == 'failed') {
errorCount += 1
if (errorCount == 3) {
Cypress.runner.stop()
cy.state('test').state = 'failed'
}
}
})
})
The afterEach() is too early to call Cypress.runner.stop(), but it seems to work with test:after:run.
describe('test body', () => {
Cypress.on('test:after:run', (result) => {
if (result.currentRetry === result.retries && result.state === 'failed') {
Cypress.runner.stop()
}
})
it('should pass first test', function() {
expect(true).to.eq(true)
})
it('should pass second test', function() {
expect(true).to.eq(true)
})
it('should fail third test and skip this script', function() {
expect(true).to.eq(false)
})
it('should skip fourth test', function() {
expect(true).to.eq(true)
})
})
Running two identical tests in a single run
Before adding the Cypress.on('test:after:run')
After adding the Cypress.on('test:after:run')
I ran your test exactly in my local with cypress version 10.0.3, I am getting the failure count. This is without test re-tries.
describe('test body', () => {
afterEach(() => {
if (cy.state('test').state == 'failed') {
Cypress.runner.stop()
}
})
it('should pass first test', function () {
expect(true).to.eq(true)
})
it('should pass second test', function () {
expect(true).to.eq(true)
})
it('should fail third test and skip this script', function () {
expect(true).to.eq(false)
})
it('should skip fourth test', function () {
expect(true).to.eq(true)
})
})
Terminal Screenshot:
In case of test retries, one way would be to manually set the test state to failed like this:
afterEach(() => {
if (cy.state('test').state == 'failed') {
Cypress.runner.stop()
cy.state('test').state = 'failed'
}
})
This question already has answers here:
node.js how to get better error messages for async tests using mocha
(2 answers)
Closed 5 years ago.
While writing tests for a project with Mocha & Chai I noticed I could get true.should.be.false to fail, but when the variable under test came from a promise and that expectation failed, Mocha would time out: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Here are the examples of things I tried (and the solution) in hopes that it'll help someone in the future.
const chai = require('chai');
const should = chai.should();
const assert = chai.assert;
function getFoo() {
return new Promise((resolve, reject) => {
resolve({bar: true});
});
}
describe('Example for StackOverflow', function() {
it('will fail as expected', function() {
true.should.be.false;
});
it('will also fail as expected', function() {
var foo = {
bar: true
};
foo.bar.should.be.false;
});
it('times out instead of fails', function(done) {
getFoo().then(data => {
data.bar.should.be.false;
done();
});
});
it('times out instead of fails even without arrow notation', function(done) {
getFoo().then(function(data) {
data.bar.should.be.false;
done();
});
});
it('should throws an error when the expectation fails, but the done() in catch() doesnt seem to matter', function(done) {
getFoo().then(data => {
data.bar.should.be.false;
done();
})
.catch(error => {
console.error(error);
done();
})
.catch(error => {
console.error(error);
done();
});
});
it('still throws an error in the catch() if I try to use assert.fail() inside the catch to force a failure', function(done) {
getFoo().then(data => {
data.bar.should.be.false;
done();
})
.catch(error => {
console.error(error);
assert.fail(0, 1);
done();
})
.catch(error => {
console.error(error);
done();
});
});
});
For reference, here are the versions at play here:
node: v5.12.0
chai: v4.1.0
mocha: v3.4.2
This is different from node.js how to get better error messages for async tests using mocha in that I'm specifically talking about a timeout that occurs when should detects a failure and throws an error that isn't caught because the promise is not returned. Their solution focuses on using done- mine does not need it because it's returning the promise to Mocha so that it can catch the error.
The solution is to return the promise in the test function - I wasn't doing that in my times out instead of fails examples, and I noticed it later while writing up my examples to ask this question.
const chai = require('chai');
const should = chai.should();
const assert = chai.assert;
function getFoo() {
return new Promise((resolve, reject) => {
resolve({bar: true});
});
}
describe('Example for StackOverflow', function() {
it('needs to return the promise for the test runner to fail the test on a thrown exception - no done necessary', function() {
return getFoo().then(data => {
data.bar.should.be.false;
});
});
});
I followed the WebStorm video on how to setup Mocha in WebStorm:
https://www.youtube.com/watch?time_continue=81&v=4mKiGkokyx8
I created a very simple test with a pass and a fail:
var assert = require("assert")
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -'), function() {
assert.equal(-1, [1,2,3].indexOf(5))
}
it('should fail'), function() {
assert.equal(1, [1,2,3].indexOf(5))
}
})
})
I then setup a run configuration like this:
And then I run it. It just states that the tests are 'pending' and then the process completes:
Why is this happening?
Your both tests are ignored, because you are using incorrect it() syntax. Please try changing your suite as follows:
var assert = require("assert")
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -', function() {
assert.equal(-1, [1,2,3].indexOf(5))
})
it('should fail', function() {
assert.equal(1, [1,2,3].indexOf(5))
})
})
})
Mocha multiple test in order
I am writting test with mocha like this
describe('application', function(){
describe('First set of test', function(){
var socket;
before(function(done) {
var opts = {
'reconnection delay' : 0,
'reopen delay' : 0,
'forceNew' : true
};
socketio = io.connect(SOCKET_URL, opts);
done();
socketio.on('connect', function() {
console.log('worked...');
});
socketio.on('disconnect', function() {
console.log('disconnected...');
})
});
it('first test should be true', function(done){
expect(variable_one).to.be.false;
done();
});
it('second test should be true', function(done){
client.hget(hash, "status", function(err, result){
expect(variable_two).to.be.true;
done();
})
});
it('thrid test should be true', function(done){
client.hget(hash, "status", function(err, result){
expect(variable_three).to.be.true;
done();
})
});
});
});
What I need is that test should be nested in order, to set the before variables is not enough because the third test depends on the second and the second on the first one. With the done variable are all running in parallel.
Is there a posible way to achieve this?
Thank you
I have two function:
test.set(object details)
test.onChanged.addListener(function(object info) {...})
fired onChanged when a test is set.
So, how should I use qunit and promise to test it?
QUnit.asyncTest("onChanged with set", function(assert) {
new Promise(function(r1) {
test.set({});
r1();
}).then(function() {
new Promise(function(r2) {
test.onChanged.addListener(function(info) {
assert.ok(true, "onChanged callback");
assert.deepEqual(info, {}, "onChanged result");
r2();
});
});
}).then(function() {
QUnit.start();
});
});
But, it does not run.