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 是如何工作的。可参考 Hacking Gaia 来开始学习 Gaia。

运行单元测试

您可以在  B2G desktop 或 Firefox Nightly 上运行单元测试。您也需要最新的 Gaia 仓库。为了能够执行绝大部分功能,您必须要安装 Node.js 和 NPM

注意: 当在安装 test-agent 依赖时,如果下面的命令会因为一种神秘的错误而失败,可能是由于您的 Node.js/NPM 版本太老了。请阅读 通过包管理器来安装 Node.js 以安装最新的版本,并且将 /tools/test-agent/node_modules 文件夹删掉。

在 Gaia 仓库中有一个 bin/gaia-test 脚本文件, 可以帮助我们以简单的方式运行测试。

小心: this script will generate a profile suited for unit tests in profile-debug. If you already have another profile in this directory it will be overwritten. Bug 980496 aims to make this configurable.

在 Firefox 中启动 test runner

This will run the test server and launch your default Firefox as found in the path:

bin/gaia-test

选择 Firefox 二进制

You can export the FIREFOX environment variable to your firefox binary. For example, on MacOS X:

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

Alternatively, you can pass it as argument to bin/gaia-test:

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

使用 B2G Desktop 启动 test runner

This will download and launch B2G Desktop:

bin/gaia-test -d

Run the tests from the Web Interface

You can simply click on specific tests and then the Execute button.

Run the tests from the command line

With the WebSocket server running, and the Test Agent app running in B2G Desktop/Firefox Nightly, run the following command:

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

You can also optionally provide a reporter to format the test output:

make REPORTER=List test-agent-test
Note: Not all reporters work, since we currently do not support 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.

设置您的 Gaia 应用

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.

Using mocks

TBD

进阶: 脚本在做什么?

产生一个 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.

启动 WebSocket 服务器

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.

在 Firefox OS 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 Mac OSX, 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.

使用 B2G Desktop 运行单元测试

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

进阶: test-agent 如何工作?

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

文档标签和贡献者

 此页面的贡献者: chrisdavidmills, ReyCG_sub
 最后编辑者: chrisdavidmills,