跳過 / 停用測試
概述
若要防止測試模組執行,只需將該模組中的 disabled
屬性設定為 true
,如下所示
tests/sampleTest.js
module.exports = {
'@disabled': true, // This will prevent the test module from running.
'sample test': function (browser) {
// test code
}
};
如果您不想執行已知會失敗的某些測試,這會很有用。
跳過個別測試案例
僅在使用 BDD Describes 介面時,才支援停用/跳過個別測試案例。若要跳過測試案例,只需使用 test.skip()
、it.skip()
、xtest()
或 xit()
其中之一將其標記為跳過,這些都是等效的。
範例
tests/sampleTest.js
describe('homepage test with describe', function() {
// skipped testcase: equivalent to: test.skip(), it.skip(), and xit()
it.skip('async testcase', async browser => {
const result = await browser.getText('#navigation');
console.log('result', result.value)
});
});
如果使用預設介面,則透過簡單的變通方法即可相對簡單地達成。只需將測試方法轉換為字串,Nightwatch 就會忽略它。
以下是一個範例
tests/sampleTest.js
module.exports = {
'sample test': function (browser) {
// test code
},
// disabled
'other sample test': ''+function (browser) {
// test code
}
};
僅執行特定測試案例
如果您有興趣在整個測試套件 (即 describe()
區塊內的 it()
或 test()
函數) 中執行特定的測試案例,請使用 it.only()
或 test.only()
函數,它們是等效的。
範例
以下程式碼只會執行 startHomepage
測試案例,並忽略其餘的測試案例。
tests/sampleTest.js
describe('homepage test with describe', function() {
test.only('startHomepage', () => {
// ...
});
test('other testcase', () => {
// ...
});
});