简体   繁体   English

如何逐个运行摩卡测试模块?

[英]How to run mocha test modules one by one?

I am implementing a mocha test script to login and logout a specific webpage and my purpose is to make this test script modular. 我正在实现一个mocha测试脚本来登录和注销特定的网页,我的目的是使这个测试脚本模块化。

Actually my main test script like the following; 其实我的主要测试脚本如下;

describe('Test is being started for user : ' + 
    currentUserInfo.email , function () {
    it('Login Test', async function () {
        await loginTest(page, currentUserInfo.email, 
    currentUserInfo.password);  
    });

    it('Logout Test', async function () {
        await logoutTest(page);
    });
});

And the logintest.js like the following; logintest.js如下所示;

module.exports = function(page, userName, userPass){

    before (async function () {
    });

    after (async function () {
        console.log("Login test is finished");
    })

    describe('Login Test is being started for user : ' + 
userName , function () {
        it('Enter Email', async function () {
            await page.focus('#username_box')
            await page.keyboard.type(userName)
        });

        it('Enter Password', async function () {
            await page.focus('#password_box')
            await page.keyboard.type(userPass)
        });

        it('Click "Login in Here" button', async 
function () {
            await page.click('input[value="Log in 
Here"]'); // With type
            await page.waitForNavigation();     
        });

};

In the main test runtime the logoutTest function doesn't wait to finish the loginTest. 在主测试运行时,logoutTest函数不等待完成loginTest。 Also, I have tried to use Promise object but in this case, my scripts don't run that under the LoginTest. 此外,我曾尝试使用Promise对象,但在这种情况下,我的脚本不会在LoginTest下运行。

     module.exports = async function(page, userName, userPass){
  return new Promise(resolve => {
    before (async function () {
    });

    after (async function () {
        console.log("Login test is finished");
        resolve(10);
    })

    describe('Login Test is being started for user : ' + 
userName , function () {
        it('Enter Email', async function () {
            await page.focus('#username_box')
            await page.keyboard.type(userName)
        });

        it('Enter Password', async function () {
            await page.focus('#password_box')
            await page.keyboard.type(userPass)
        });

        it('Click "Login in Here" button', async function () {
            await page.click('input[value="Log in Here"]'); // With type
            await page.waitForNavigation();     
        });
    });
  });
};

thanks 谢谢

Mocha does run sequentially. Mocha确实按顺序运行。

your logintest.js is exporting a non-async function. 你的logintest.js正在导出一个非异步函数。 So, the await on your main test is not blocking as expected and the logout test will start before the logintest.js finishes. 因此,主要测试的await不会按预期阻塞,并且logout测试将在logintest.js完成之前启动。

Besides, I would recommend you to nest the logintest.js and loginouttest.js in the main.js inside a describe block. 此外,我建议你将logintest.js和loginouttest.js嵌套在describe块内的main.js中。

describe('main', function() {
  describe('login', function() {
    before(...)
    after(...)
    it(...)
    it(...)
  }
  describe('logout', function() {
    before(...)
    after(...)
    it(...)
    it(...)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM