【问题标题】:How to run mocha test modules one by one?如何逐个运行 mocha 测试模块?
【发布时间】:2019-11-07 03:40:37
【问题描述】:

我正在实现一个 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();     
        });
    });
  });
};

谢谢

【问题讨论】:

    标签: javascript npm mocha.js puppeteer


    【解决方案1】:

    Mocha 确实按顺序运行。

    您的 logintest.js 正在导出一个非异步函数。因此,主测试中的await 没有按预期阻塞,注销测试将在 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(...)
      }
    }
    

    【讨论】:

    • 但是在主测试中加入其他测试用例,是不是降低了代码的可读性?
    • 这取决于你的测试用例的数量。如果您有大量的测试用例,并且想将它们分开在不同的 .js 中,您仍然应该遵循嵌套描述模式以便更好地分组。实际编码风格可以参考here
    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2018-12-02
    • 2020-08-26
    • 2014-10-19
    • 2012-04-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多