繁体   English   中英

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

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

我正在实现一个mocha测试脚本来登录和注销特定的网页,我的目的是使这个测试脚本模块化。

其实我的主要测试脚本如下;

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);
    });
});

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();     
        });

};

在主测试运行时,logoutTest函数不等待完成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();     
        });
    });
  });
};

谢谢

Mocha确实按顺序运行。

你的logintest.js正在导出一个非异步函数。 因此,主要测试的await不会按预期阻塞,并且logout测试将在logintest.js完成之前启动。

此外,我建议你将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