Question

I have 2 nodeunit test cases in which if first fails i don't want to run 2nd test case.(Process should skip testing 2nd test case with proper result.) How do I do the same. I have written sample code where In first test case I have specified number of expects should be one. Even if first test case fails, second test case still executes. What could be the issue here.

 exports.testSomething = function(test){
    test.expect(1);
    test.ok(false, "this assertion should fail");
    test.done();
};

exports.testSomethingElse = function(test){
    test.ok(true, "this assertion should pass");
    test.done();
};
Était-ce utile?

La solution

Generally it is a bad idea to make separate tests dependent on eachother, and testing frameworks don't support this kind of thing because of that. Nodeunit tests are declared and processed for execution before any one test has started so there is no way to change which tests run.

For that specific example, if the add fails, then it makes sense that delete fails. It might be slightly annoying to get more output, but it's doing just what it is supposed to do.

It may not be applicable in this cause, but one option is to have a separate section for your delete tests where you use setUp to set up everything that delete needs without having to actually call add.

Update

test.expect is used to specify how many assertions were expected in a single test. You only really need it if your test has asynchronous logic. For example:

'test': function(test){
  test.expect(5);
  test.ok(true, 'should pass');

  for (var i = 0; i < 4; i++){
    setTimeout(function(){
      test.ok(false, 'should fail');
    }, 5);
  }

  test.done();
},

Without the test.expect(5), the test would pass because done is called before the line in the setTimeout has run, so the test has no way of knowing that is has missed something. By having the expect, it makes sure the test will properly fail.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top