正确处理断言错误的方法,以便后续测试可以运行

时间:2017-10-04 09:16:55

标签: javascript node.js mocha chai assertions

处理任何形式的错误/断言错误的正确方法是什么,以便当一个测试用例失败时,后续测试仍会运行?或者在1个JavaScript文件中只有1个测试用例对我来说更好吗?

describe('SMP Service Login Page', function() {
    describe('Loggin in', function() {
        // Unhappy flow always first
        var assert = require('assert');
        var useridField;
        var passwordField;
        var loginButton;
        it('should not allow user to login with wrong userid', function(done) {
            throw new Error("haha");

        });        
        it('should fail because of assertion', function(done) {            
            useridField = browser.element('android=new UiSelector().text("User Name").className("android.widget.EditText")');                  
            useridField.setValue('FIRST');
            console.log(useridField);

            passwordField = browser.element('android=new UiSelector().className("android.widget.EditText").instance(1)');                
            passwordField.setValue('FIRST');

            loginButton = browser.element('android=new UiSelector().resourceId("android:id/button1")');
            var text = loginButton.getText(); //text = 'LOG IN'            
            assert.equal(text,'LOG'); //this should fail , throws an error    
            loginButton.click(done);
        });
//this test doesn't get run cause of assertion error thrown

        it('should pass but it fails because it could not locate the useridField', function(done) {            
            console.log(useridField);
            useridField.setValue('userid'); 

            passwordField = browser.element('android=new UiSelector().className("android.widget.EditText").instance(1)');            
            passwordField.setValue('passsword'); 

            loginButton = browser.element('android=new UiSelector().resourceId("android:id/button1")');            
            loginButton.click(done);        
        });
    });
});

1 个答案:

答案 0 :(得分:2)

最好的方法是将测试编写为彼此独立的测试。在您显示的最后一次测试的代码中,不应该依赖于先前的测试来设置useridField。您可以将初始化它的代码移动到beforeEach挂钩。