テストランナーとして 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