最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to continue a test case when an assertion failed in CasperJS? - Stack Overflow

programmeradmin2浏览0评论

Is there a way for continue a test suite when a fail occurs? By example :

casper.test.begin("",3,function suite(){
  casper.start(url).then(function(){
    test.assert(...);
    test.assert(...); //If this assert fail, the script stop and the third assert isn't tested
    test.assert(...);
  }).run(function(){
    test.done();
  });
});

I want all assert are tested, even if some fail. Is it possible?

Is there a way for continue a test suite when a fail occurs? By example :

casper.test.begin("",3,function suite(){
  casper.start(url).then(function(){
    test.assert(...);
    test.assert(...); //If this assert fail, the script stop and the third assert isn't tested
    test.assert(...);
  }).run(function(){
    test.done();
  });
});

I want all assert are tested, even if some fail. Is it possible?

Share Improve this question edited Jan 4, 2015 at 12:57 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Jan 3, 2015 at 23:08 spacecodeurspacecodeur 2,4167 gold badges43 silver badges77 bronze badges 1
  • For unit testing this behaviour is what you would usually want. You might use "verification" approach, which checks but not stops the test execution (like suggested in the answer below). Having this situation like you describe usually signals about bad test design. Testing too much in one test. The solution would be split it into separate tests. – buxter Commented Jan 6, 2015 at 11:50
Add a ment  | 

2 Answers 2

Reset to default 6

See in casperjs google group post. We can surround the assert with the casper.then(..

This follow code works like I want (but this way isn't maybe the best?)

casper.test.begin("",3,function suite(){
  casper.start(url).then(function(){
    casper.then(function(){
      test.assert(...); //if fail, this suite test continue
    });
    casper.then(function(){
      test.assert(...); //so if assert(1) fail, this assert is executed
    });
    casper.then(function(){
      test.assert(...);
    });
    }).run(function(){
      test.done();
    });
});

This is normally what you want when unit testing: if it is going to fail anyway, do it quickly. I.e. fail at the first problem in each test function. Also, later tests usually assume earlier tests passed, e.g. if the page title is wrong and saying 404, there is no point testing that the correct number of images are on the page.

I am guessing you want this so that you can get more information in the test results, and one way to do that would be to use a single assert and a custom error message:

var title = this.getTitle();
var linkText = this.getHTML('a#testLink');
this.assert( title == "MyPage" && linkText == "continue",
  "title=" + title + ";a#testLink = " + linkText);

But that can get messy. If you want to use all the power of the assert family of functions, and not have them throw, but instead continue, a study of the source code shows that this might work:

test.assert(false, null, {doThrow:false} );
test.assertEquals(1 == 2, null, {doThrow:false} );
test.assertEquals(2 == 2);

And if you want this to be the default behaviour on all your asserts, well, hacking the code might be the best choice! (Change the default of true for doThrow to be false.)

发布评论

评论列表(0)

  1. 暂无评论