Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Gaiaユニットテスト

Gaia/B2Gソースコードの一部として、GaiaとB2Gの異なる側面をテストするために、さまざまなユニットテストが使用可能になった。この記事ではこれらにアクセスする方法について解説します。

注意: このドキュメントではあなたがGaiaとB2Gを動かす方法を理解していると仮定しています。Gaiaの開発を開始するにはGaiaの開発を見てください。

ユニットテストを実行する

B2GデスクトップFirefox Nightlyでユニットテストを実行することができます。また最新のGaiaリポジトリが必要です。ほとんどの機能のためにNode.jsNPMがインストールされている必要があります。

注意: もし以下のコマンドがtest-agentの依存関係をインストール中に暗号に関するエラーで失敗したら、Node.js/NPMのバージョンが古すぎることに起因しているかもしれません。新しいバージョンをインストールしnode_modules/test-agentフォルダを削除するためにパッケージマネージャによってNode.jsをインストールするを読んでください。

Gaiaリポジトリにはテストの実行が簡単になるように便利なbin/gaia-testスクリプトが同梱されます。

注意してください:このスクリプトはprofile-debugのユニットテストに適したプロファイルを生成します。すでにこのディレクトリに別のプロファイルがある場合それを上書きします。Bug 980496ではこれをコンフィグ可能にすることを目指しています。

Firefoxでテストランナーを起動する

テストサーバとPATHから見つかるデフォルトのFirefoxを起動します:

bin/gaia-test

Firefoxのバイナリを選択する

FIREFOX環境変数にFirefoxバイナリのパスをexportすることができる。例えばOS Xの場合:

export FIREFOX=/Applications/FirefoxNightly.app/Contents/MacOS/firefox

或いは、bin/gaia-testへの引数として渡すこともできます:

bin/gaia-test <gaia directory> <firefox path>

B2Gデスクトップでテストランナーを起動する

ダウンロードとB2Gデスクトップの起動:

bin/gaia-test -d

Webインターフェイスからのテスト実行

特定のテストとExecuteボタンをシンプルにクリックすることでできます。

コマンドラインからのテスト実行

以下のコマンドでWebSocketサーバを実行し、B2G Desktop/Firefox Nightlyでテストエージェントアプリを実行します:

make test-agent-test

If you only want to run one app's tests you can specify which via the APP env variable:

make test-agent-test APP=calendar

また必要に応じてテスト出力を整形するためのレポータを提供することができます:

make REPORTER=List test-agent-test
注意:現在Docをサポートしていないので、全てのリポータが動くわけではありません。

Run the tests as you save

When the server is running, the tests for a file are run automatically when a file is saved or even just touched:

  • When you save a test file, the test file itself is run
  • When you save another file, it finds a matching file in the test/unit directory, by suffixing the file name with _test.js.

Note: It watches only existing files so if you create a new file, you have to restart the agent.

Running tests like TBPL does

Gaia unit tests in TBPL are run using a separate runner; this explains how to use it.  Please consult the virtualenv docs if you're not familiar with using a Python virtualenv.

virtualenv venv
source venv/bin/activate
cd $GAIA/tests/python/gaia-unit-tests
python setup.py develop
cd gaia_unit_test
python main.py --binary /path/to/b2g/desktop/build --profile /path/to/gaia/profile

Note: When specifying the path to the B2G desktop build, you should specify the path to b2g-bin, if it exists, otherwise use b2g.

The Gaia profile must be made using the following:

NO_LOCK_SCREEN=1 DEBUG=1 DESKTOP=0 make

By default, this profile will be generated in $GAIA/profile-debug. bin/gaia-test generates the same profile so you don't need to regenerate it if you already run gaia-test.

Disabling a gaia unit test in TBPL

TBPL uses a blacklist to exclude certain gaia unit tests from being run.  To prevent a test from running in TBPL, add its path to https://github.com/mozilla-b2g/gaia/blob/master/tests/python/gaia-unit-tests/gaia_unit_test/disabled.json.

Setting up your Gaia app

Although this guide should help make things easier, the best way to learn how to write, set up, and run tests is currently still to look at the source code; in particular, take a look at the gallery tests.

Loading your own test files

Note: This is an advanced and entirely optional technique. If test-agent's native test loader is working for your application and your test style, and you don't want to use your own test files, this section is not for you!

Test-agent loads your test files in an environment where they have access to mocha (among other bits of global state). One important test-agent global is require, which allows your test file to load a dependency (application code for instance). Sometimes, particularly if your application files expect to be loaded in a way that's different from test-agent's require, it can be useful to override test-agent's test file loader. You can do so in the following manner:

// setup.js

testAgentRuntime.testLoader = function(path) {
  // We're going to return a promise to represent
  // the loading state of the test file at this path.
  return new Promise((accept, reject) => {
    // Load the parameter test file and resolve (or reject) appropriately
    // ...
  });
};

Using mocks

TBD

Advanced: what does the script do?

Generating a profile

You need a profile that is generated by this command:

NO_LOCK_SCREEN=1 DEBUG=1 DESKTOP=0 make

This generates a debug profile in gaia/profile-debug, overriding a previous profile if you already have one.

  • DEBUG=1 enables the httpd.js extension that makes it possible to directly use the files from the apps/ directory.
  • NO_LOCK_SCREEN=1 disables the lock screen, which is necessary in B2G Desktop because it's not possible to unlock it using the mouse.
  • DESKTOP=0 disables the other addons we normally use in DEBUG mode to run Gaia in Firefox.

Launch the WebSocket server

Test agent (the test runner) ships with a built in WebSocket server that lets you remotely message the browser or device to queue a test run. Often you will want to develop with time saving features like a file watcher that will run your tests when a test file or implementation changes. To take advantage of these features you need to start the server:

make test-agent-server

Using the WebSocket server provides other tools such as a command line reporter for test results (watch the terminal you ran the command from), a Growl reporter, syntax error notifications, and more.

The agent also watches for modifications in files, and automatically runs the associated tests. It runs when you save the test or if you save the tested file (we use the convention where the test filename is the tested filename with _test appended, see below for more examples). It watches only existing files so if you create a new file, you have to restart the agent.

Running unit tests with Firefox Nightly

You can launch Gaia in Firefox Nightly with the following commands:

cd <path to gaia>
<path to nightly>/firefox --no-remote -profile <path to gaia>/profile-debug/ https://test-agent.gaiamobile.org:8080/

Note: In OS X, The profile path should be absolute path

You can use Firebug or the integrated debugger to debug the tests; use the debugger keyword to break in the debugger.

Running unit tests with B2G Desktop

Launch Gaia and start the "Test Agent" app. From the Test gent app you can select tests to run from the UI.

Advanced: how does the test-agent work?

The Test Agent lives in its own Github repository. You can have look there to understand how it works under the hood.

ドキュメントのタグと貢献者

 このページの貢献者: hamasaki, aoitan
 最終更新者: hamasaki,