使用 Mocha 作為測試執行器

概述

在 Nightwatch 2 中,整合的 Mocha 執行器已升級為使用 Mocha v9,並且實作也已更新,以符合 Nightwatch 在其預設測試執行器中提供的大部分功能,例如使用標籤或全域測試鉤子的能力。

為什麼選擇 Mocha?

即使 Nightwatch 從 1.3 版本開始就支援使用 BDD describe 介面撰寫測試,但鑑於 Mocha 的極高人氣、悠久歷史和易用性,它仍然是一個吸引人的選擇。

Mocha 對進階報表工具的支援仍然無與倫比,因此我們竭盡所能確保 Mocha 在 Nightwatch 2 中能更好地運作。

組態設定

為了在 Nightwatch 中使用 Mocha,您需要設定 test_runner 組態屬性,並將類型設定為 mocha。也可以指定 Mocha 的自訂選項

nightwatch.conf.js
{
  // other settings...
  test_runner: {
    type : 'mocha',
    options : {
      ui : 'bdd',
      reporter : 'list'
    }
  }
}

或簡化為

nightwatch.conf.js
{
  test_runner : 'mocha'
}

可以在這裡找到受支援的完整 Mocha 選項清單。

test_runner 選項也可以在測試環境層級中指定

nightwatch.conf.js
{
  test_settings : {
    default: {
      test_runner: 'default'
    },
    
mocha_tests: { test_runner : { type : "mocha", options : { ui : "bdd", reporter : "list" } } } } }

CLI 選項

Nightwatch 支援某些以主要 nightwatch CLI 工具的引數形式指定的 Mocha 特定 CLI 選項。其中某些選項(例如 retries)也在 Nightwatch 中定義了行為,並且在使用 Mocha 時,Nightwatch 將會委派它們。

以下是目前受支援的引數清單

  • --reporter
  • --grep
  • --fail-fast - 在 Mocha 中定義為 --bail
  • --retries
  • --fgrep
  • --invert

範例:

npx nightwatch examples/tests/ --reporter mochawesome

擴充的 describe() 語法

Nightwatch 2 中新的 Mocha 支援的建置方式,盡可能符合內建 Nightwatch describes() 語法中可用的擴充語法。

以下是在 Nightwatch 中使用 Mocha 時可用的完整語法

describe('homepage test with describe', function() {
  // All current settings are available via this.settings
  // console.log('Settings', this.settings);
  
// All current cli arguments are available via this.argv // console.log('argv', this.argv);
// The current mocha options object // console.log('mochaOptions', this.mochaOptions);
// All current globals are available via this.globals // console.log('globals', this.globals);
// testsuite specific capabilities // this.desiredCapabilities = {};
// Enable this if the current test is a unit/integration test (i.e. no Webdriver session will be created) // this.unitTest = false
// Set this to false if you'd like the browser window to be kept open in case of a failure or error (useful for debugging) // this.endSessionOnFail = true
// Set this to false if you'd like the rest of the test cases/test steps to be executed in the event of an assertion failure/error // this.skipTestcasesOnFail = true
// this.suiteRetries(2);
// Control the assertion and element commands timeout until when an element should be located or assertion passed // this.waitForTimeout(1000)
// Control the unit test timeout // this.timeout(1000)
// Controll the polling interval between re-tries for assertions or element commands // this.waitForRetryInterval(100);
before(function(browser) { this.homepage = browser.page.home(); });
it('startHomepage', () => { this.homepage.navigate(); this.homepage.expect.section('@indexContainer').to.be.not.visible; });

// Run only this testcase // it.only('startHomepage', () => { // this.homepage.navigate(); // });
// skipped testcase: equivalent to xit() it.skip('async testcase', async browser => { const result = await browser.getText('#navigation'); console.log('result', result.value) });
after(browser => browser.end()); });

範例

在 Mocha 中撰寫測試與在 Nightwatch 中撰寫測試相同。每個測試案例都會接收 browser 物件,hooks 也會接收用於非同步操作的 done 回呼。

tests/sampleTest.js
describe('Google demo test for Mocha', function() {
  
describe('with Nightwatch', function() {
before(function(browser, done) { done(); });
after(function(browser, done) { browser.end(function() { done(); }); });
afterEach(function(browser, done) { done(); });
beforeEach(function(browser, done) { done(); });
it('uses BDD to run the Google simple test', function(browser) { browser .url('https://google.com') .expect.element('body').to.be.present.before(1000);
browser.setValue('input[type=text]', ['nightwatch', browser.Keys.ENTER]) .pause(1000) .assert.containsText('#main', 'Night Watch'); }); }); });

使用 mochawesome 報表工具

Mochawesome 是一個非常受歡迎的自訂報表工具,可與 Mocha 搭配使用,並且在將 Mocha 用作測試執行器時,它也可以與 Nightwatch 開箱即用。

若要使用 Mochawesome,只需使用上述資訊將 Mocha 設定為 test_runner,然後使用 NPM 從安裝它

npm i mochawesome --save-dev

若要將其用作報表工具,只需傳遞 --reporter mochawesome 引數,如下所示

npx nightwatch examples/tests/ --reporter mochawesome

設定報表工具選項

Mochawesome 報表工具選項可以在 test_runner 內的 reporterOptions 字典下的主要 Nightwatch 設定中定義

nightwatch.conf.js

{
  // ...
  test_runner: {
    type : 'mocha',
    options : {
      ui : 'bdd',
      reporter : 'mochawesome',
      reporterOptions: {
        reportDir: './output'
      }
    }
  }
}

平行執行

當使用測試工作者平行執行測試時,您需要安裝 mochawesome 需要的其他套件

npm install mochawesome-report-generator mochawesome-merge --save-dev

使用 mocha-junit-reporter

當使用 Mocha 時,Nightwatch 中預設的內建 JUnit 報表工具無法使用,但一個很好的替代方案是改用受歡迎的 mocha-junit-reporter

您只需要從 NPM 安裝它就可以開始使用。您可以選擇在需要時,以與 mochawesome 報表工具相同的方式設定其設定

nightwatch.conf.js
{
  // ...
  test_runner: {
    type : 'mocha',
    options : {
      reporterOptions: {
        reportDir: './output'
      }
    }
  }
}
npm i mocha-junit-reporter --save-dev

若要將其用作報表工具,只需傳遞 --reporter mocha-junit-reporter 引數,如下所示

npx nightwatch examples/tests/ --reporter mocha-junit-reporter