GUIアプリケーションにおいてユーザーインターフェースのテストは重要です。今回はUnityとUbuntuのインストーラーのUIテスト自動化のために開発された、
Autopilotとは
冒頭でも述べたとおりUIのテストは重要なのですが、
しかしこれでは作業コストがかかりすぎてしまいますし、
そこで開発されたのがAutopilotです。AutopilotはPythonで書かれた、
また、
Autopilotを使うと、
インストールとUnityのテスト
それではまずAutopilotをインストールしましょう。Autopilotの動作にはUbuntu 12.
$ sudo apt-get install python-autopilot unity-autopilot
最後のunity-autopilotパッケージは、
$ autopilot list unity unity.tests.launcher.test_capture.LauncherCaptureTests.test_launcher_capture_while_not_sticky_and_hidden unity.tests.launcher.test_capture.LauncherCaptureTests.test_launcher_capture_while_sticky_and_hidden_moving_right unity.tests.launcher.test_capture.LauncherCaptureTests.test_launcher_captures_while_sticky_and_revealed unity.tests.launcher.test_capture.LauncherCaptureTests.test_launcher_not_capture_while_not_sticky_and_hidden_moving_right unity.tests.launcher.test_capture.LauncherCaptureTests.test_launcher_not_capture_while_not_sticky_and_revealed (以下略)
同じテストを複数のテストパターン
実際にいくつかのテストを試してみましょう。UIテスト時は、
ゲストセッションに移行したらrunコマンドに実行したテストIDを指定します。端末を開いて次のコマンドを実行してください。
$ autopilot run unity.tests.test_dash.DashKeyNavTests.test_lensbar_enter_activation . ---------------------------------------------------------------------- Ran 1 test in 7.302s OK
これは、
キーボードやマウスの操作をエミュレートできるため、
$ autopilot run unity.tests.test_ibus.IBusTestsAnthy.test_anthy ............ ---------------------------------------------------------------------- Ran 12 tests in 86.568s OK
ちなみにunityテストスイートのすべてのテストを実行する場合は、
$ autopilot run unity
もっと簡単なサンプル
今度は実際にテストを作ってみましょう。Pythonの知識さえあれば誰でもかんたんに作成できます。ユニットテストに触れた経験があるなら、
まず、
$ sudo add-apt-repository ppa:autopilot/ppa $ sudo apt-get update $ sudo apt-get upgrade もしくは $ sudo apt-get python-autopilot
ひな形を作る
Autopilotでは、
そこでファイルの構成としてまずテストスイート名のフォルダーを作り、
以下のようなフォルダーとファイルを作成してください。
.
└── Recipe
├── __init__.py
└── test_sample.py
__
編集すべきファイルはtest_
# -*- coding: utf-8 -*-
from autopilot.testcase import AutopilotTestCase
class SampleTests(AutopilotTestCase):
def test_keyboard(self):
self.keyboard.type("Hello autopilot")
たったこれだけで
$ autopilot list Recipe Loading tests from: /home/shibata/Sample Recipe.test_sample.SampleTests.test_keyboard 1 total tests.
runコマンドで実行すると
$ autopilot run Recipe.test_sample.SampleTests.test_keyboard Loading tests from: /home/shibata/Sample Tests running... Hello autopilot Ran 1 test in 3.200s OK
setUp()とtearDown()
一つのテストケースに複数のテストメソッド
そんな時に使えるのがsetUp()とtearDown()と呼ばれるテストフィクスチャーです。テストケースのクラスでオーバーライドすることで、
前節の例では実行した端末上でキーボード入力を行っていました。しかし本来は端末上ではなく、
# -*- coding: utf-8 -*-
from autopilot.testcase import AutopilotTestCase
class SampleTests(AutopilotTestCase):
def setUp(self):
super(SampleTests, self).setUp()
self.app = self.start_app("Text Editor")
def test_keyboard(self):
self.keyboard.type("Hello autopilot")
テストを実行してみると、
マウスの操作
マウス操作用のクラスも存在します。test_
def test_mouse(self):
self.keyboard.press_and_release("Super+Ctrl+Up")
self.mouse.move(0, 0)
self.mouse.click(button=1)
新しく追加したテストを実行すると、
$ autopilot run Recipe.test_sample.SampleTests.test_mouse
keyboard.
テスト結果の確認
ここまでの作業では
Autopilotでは各GUIツールキットのイントロスペクションを提供することで、
また、
例えば、
from autopilot.testcase import AutopilotTestCase
from autopilot.emulators.clipboard import get_clipboard_contents
from autopilot.matchers import Eventually
from testtools.matchers import Equals
(中略)
def test_clipboard(self):
self.keyboard.type("Hello autopilot")
self.keyboard.press_and_release("Ctrl+a")
self.keyboard.press_and_release("Ctrl+c")
self.assertThat(get_clipboard_contents, Eventually(Equals("Hello autopilot")))
新しく追加したテストを実行しましょう。画面上で文字
$ autopilot run Recipe.test_sample.SampleTests.test_clipboard Loading tests from: /home/shibata/Sample Tests running... Ran 1 test in 5.277s OK
まとめ
AutopilotでUIテストを書くための必要最低限の知識を紹介しました。これをもとにそれぞれのアプリケーションごとにテストを書き始めるわけですが、
前者については、
UIテストとしての機能もまだ十分ではありませんが、
まだ機能が複雑でないうちに、