[英]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.