受け入れテストを実行する

提供:MoodleDocs
移動先:案内検索

テンプレート:Migrated

The translation work is aborted because it has been set to automatically forward to the following page: https://moodledev.io/general/development/tools/behat/running

Moodleではテストツールの一部として自動機能テスト用PHPフレームワークBehatを使用しています。

Behatは一連の「フィーチャ (feature)」「シナリオ (scenario)」「ステップ (step)」を取得します。これらを使用してアクションを起こした後、実際のウェブブラウザで結果をテストします。これはWebdriverと呼ばれる最新のウェブブラウザに実装されているプロトコルにより実現されます。

このドキュメントではMoodle内でBehatテストを実行する方法について要件、セットアップ、便利なヒントおよび基本的なトラブルシューティングを含みます。

必要条件 Requirements

  1. サポートされているバージョンのMoodleがインストールされた最新のOS
  2. 最新のウェブブラウザ (私たちはFirefox, Chromeを標準的にサポートしますが、他のウェブブラウザでも可能です)
  3. あなたのウェブブラウザへのWebDriverの実装
  4. 最新バージョンのSelenium (任意ですが、推奨です)
  5. 最新のJava Runtime環境 (Seleniumを使用する場合、必須です)

推奨追加設定 Recommended extras

Some extra software is also recommended for testing with Behat. Behatを使用したテストにはいくつかのソフトウェアの追加をお勧めします。

ブラウザプロファイル事前設定: moodle-browser-config Pre-configured browser profiles: moodle-browser-config

Available for all supported versions of Moodle, the moodle-browser-config is a recommended inclusion for Behat. This configuration tooling provides a range of standard browser profiles for testing. すべてのサポートされたバージョンのMoodleで利用可能なmoodle-browser-configはBehatへの実装を推奨します。この設定ツールはテスト用の標準ブラウザプロファイルを提供します。

Note: In future, the moodle-browser-config tool may be included as composer dependency to Moodle but this is currently not the case. メモ: 将来的にmoodle-browser-configツールはMoodleのコンポーザ依存として実装されるかもしれませんが、現在は実装されていません。

インストレーション Installation
  1. Check out the moodle-browser-config repository:
  2. moodle-browser-configリポジトリをチェックしてください:
   // ディレクトリをあなたのgitルートに変更してください:
   $ cd ~/git
   // moodle-browser-configリポジトリをクローンしてください。
   git clone https://github.com/andrewnicols/moodle-browser-config
  1. Open your Moodle installation's config.php in your preferred editor, and require the tool's init.php:
  2. あなたのMoodleインストレーションのconfig.phpをお好みのエディタで開いてツールのinit.phpをrequireしてください:
   require_once('/path/to/your/git/dir/moodle-browser-config/init.php');
提供プロファイル Provided profiles

The full list of profiles which are included with moodle-browser-config are provided in its own documentation. moodle-browser-configに含まれるプロファイルの完全なリストは自ドキュメンテーションで提供されています。

The following is a summary of the profiles that most users may be interested in. 以下、多くのユーザが興味を持つであろうプロファイルの概要です。

You can also provide your own custom profiles, including for remote services such as Browserstack, and Saucelabs, as well as for other browsers supporting the W3C Webdriver specification. あなたはまたBrowserstackまたはSaucelabsのようなリモートサービス用に独自カスタムプロファイルを提供できます。同様にW3C Webdriver仕様をサポートする他のブラウザ用等、独自カスタムプロファイルの提供も可能です。

Please note that Safari and Safaridriver are not currently supported as they do not meet the W3C WebDriver specification. SafariおよびSafaridriverはW3C WebDriver仕様に準拠していないため、現在はサポートされていない点に留意してください。

プロファイル名 Profile name 説明 Description Selenium使用の有無 Uses Selenium? GUI表示の有無 Displays GUI?
firefox Selenium経由でFirefoxを使用する Use Firefox via Selenium Yes Yes
headlessfirefox Selenium経由でFirefoxを使用する (GUI表示なし) Use Firefox via Selenium, without displaying the GUI Yes No
geckodriver GeckodriverでFirefoxを直接使用する Use Firefox with Geckodriver directly No Yes
headlessgeckodriver GeckodriverでFirefoxを直接使用する (GUI表示なし) Use Firefox with Geckodriver directly, without displaying the GUI No No
chrome Selenium経由でChromeを使用する Use Chrome via Selenium Yes Yes
headlesschrome Selenium経由でChromeを使用する (GUI表示なし) Use Chrome via Selenium, without displaying the GUI Yes No
chromedriver ChromedriverでChromeを直接使用する Use Chrome with Chromedriver directly No Yes
headlesschromedriver ChromedriverでChromeを直接使用する (GUI表示なし) Use Chrome with Chromedriver directly, without displaying the GUI No No
edge Selenium経由でEdgeを使用する Use Edge via Selenium Yes Yes
headlessedge Selenium経由でEdgeを使用する (GUI表示なし) Use Edge via Selenium, without displaying the GUI Yes No
edgedriver EdgedriverでEdgeを直接使用する Use Edge with Edgedriver directly No Yes
headlessedgedriver EdgedriverでEdgeを直接使用する (GUI表示なし) Use Edge with Edgedriver directly, without displaying the GUI No No

chromedriver-wrapper

When using Google Chrome, you must use the correct version of the chromedriver browser driver for the version of Chrome that you use. Google Chromeを使用する場合、あなたは使用しているChromeのバージョンに合致したバージョンのchromedriverブラウザドライバを使用する必要があります。

Since Google Chrome automatically updates on a regular basis, you will need to regularly upgrade your driver to match the version of Chrome that you are using. Google Chromeは定期的に自動更新されるため、あなたが使用中のChromeのバージョンに合わせて定期的にドライバをアップグレードする必要があります。

To make this easier, a chromedriver-wrapper utility has been written. This inspects the version of Chrome that is in your path, and downloads the correct version of chromedriver for that version, before starting it. これを簡単にするためにchromedriver-wrapperユーティリティが書かれました。これはあなたのパスにあるChromeのバージョンを検査してchromedriverを起動する前にそのバージョンに適したchromedriverをダウンロードします。

Installation instructinos can be found at https://github.com/andrewnicols/chromedriver-wrapper. インストール手順は次をご覧ください: https://github.com/andrewnicols/chromedriver-wrapper.

はじめに (基本) Getting started (basic)

This is a quick walk through to get Behat running for the first time. ここではBehatを初めて動作させるための簡単な手順を説明します。

セットアップ Setting up

There are a number of ways of configuring Behat on Moodle. This is one of the simplest. Moodle上でBehatを設定する方法はいくつかあります。これは最も簡単な方法の1つです。

These notes assume that you have already installed a supported Java Runtime Environment, and the Running_acceptance_test#Pre-configured_browser_profiles:_moodle-browser-config tool. これらの注意事項はあなたが既にサポートされたJava Runtime EnvironmentおよびRunning_acceptance_test#Pre-configured_browser_profiles:_moodle-browser-configツールをインストール済みであることを前提としています。

Seleniumをセットアップする

私たちは一般的にはSeleniumの使用を推奨しますが、これは固定要件ではありません。あなたはブラウザのドライバ実装を直接使用できますが、初めての場合はセットアップが難しくなります。

SeleniumはJavaで書かれいるため実行するには最新バージョンのJREが必要です。開始する前にJREがインストールされていることを確認してください。

Moodle 3.9.5 / 3.10.2 / 3.11.0以降、Moodleはすべての最新バージョンのSeleniumで動作します。この記事を書いている時点では3.141.59および4.1.0です。 注意:' バージョン4のjarには問題があるようです - 特に4.1.0では "Could not open connection: Capability value must be set" エラーが発生しますが、これは SeleniumおよびJavaScriptテスト用MInkExtensionの間の問題のようです。詳細は https://githubmemory.com/repo/Behat/MinkExtension/activity をご覧ください。最も簡単な解決方法は以前のバージョン (3.141.59) を使用することです。

  1. https://www.selenium.dev/downloads/からSeleniumサーバ (Grid) をダウンロードしてください。これは1つのJARファイルです。適切な場所に配置してください。
  2. Start Selenium
   # Version 3.141.59:
   $ java -jar selenium-server-standalone-3.141.59.jar
   
   # Version 4.0.0 and later:
   $ java -jar selenium-server-4.0.0-beta-3.jar standalone

あなたは使用しているSeleniumのバージョンに応じて、実行するポートを含む多くの設定を任意に指定できます。 あなたが使用しているSeleniumのバージョンのヘルプを参照してください。

あなたのブラウザを設定する Setting up your browsers

Selenium is just an intelligient wrapper to start, and manage your browser sessions. It doesn't actually include any web browsers itself. Seleniumはあなたのブラウザセッションを開始および管理するためのインテリジェントなラッパです。実際にはウェブブラウザ自体は含まれていません。

Moodle HQ run all behat tests against both Firefox and Chrome multiple times per day. Other combinations, including Microsoft Edge, are also supported. Moodle HQはFirefoxおよびChromeに対して1日に複数回すべてのBehatテストを実施します。Microsoft Edgeを含む他の組み合わせもサポートしています。

To use Behat, you will need a recent version of your preferred browser, as well as a driver for that browser. The driver is responsible for communication between Selenium (or Moodle directly) and the browser. Behatを使用するためには、あなたが使用するブラウザの最新バージョンおよびそのブラウザ用のドライバが必要です。ドライバはSelenium (またはMoodle直接) とブラウザ間の通信を担当します。

Both the browser, and its driver, must be placed inside your $PATH - this may be somewhere like /usr/bin, /usr/local/bin, or perhaps a user bin directory like ~/bin which is present in your $PATH ブラウザおよびそのドライバも共に$PATHの中に置く必要があります。$PATHは「/usr/bin」「/usr/local/bin」のような場所か、$PATHに存在する「~/bin」のようなユーザbinディレクトリかもしれません。

Chrome

あなたは次のページでGoogle Chromeをダウンロードできます: https://www.google.com/chrome.

あなたはドキュメントにあるようにchromedriverの正しいバージョンが必要です。 代わりにあなたは推奨されるエクストラセクションに記載されているchromedriver-wrapper utilityも使用できます。

Either the chromedriver binary must be in a directory in your $PATH, or the chromedriver-wrapper/bin folder must be in your $PATH. chromedriverrバイナリが$PATH内のディレクトリにあるか、chromedriver-wrapper/binフォルダが$PATH内になければなりません。

Firefox

You can download Mozilla Firefox from https://www.mozilla.org/en-US/firefox/new/. あなたはMozilla Firefoxを次でダウンロードできます: https://www.mozilla.org/en-US/firefox/new/

You will need the correct version of geckodriver as per the documentation. あなたはドキュメンテーションにあるように正しいバージョンのgeckodriver が必要です。

あなたの$PATHgeckodriverを置く必要があります。

Moodleをセットアップする Set up Moodle

  1. ファイル、特にbehatのために「dataroot」エリアを作成してください。
  2. あなたのMoodleのconfig.phpファイルに以下を設定してください:
   $CFG->behat_dataroot = '/path/to/the/dataroot/you/created';
   $CFG->behat_wwwroot = 'http://127.0.0.1/path/to/your/site';
   $CFG->behat_prefix = 'beh_';
  1. あなたがまだbehat-browser-configをインクルードしていない場合、私たちはインクルードをお勧めします。
   require_once('/path/to/moodle-browser-config/init.php');
behat_wwwrootに関するメモ Notes about the behat_wwwroot

You will need to set the behat_wwwroot to your Moodle site, but it must use a different value to your $CFG->wwwroot. あなたのMoodleサイトにbehat_wwwrootを設定する必要がありますが、$CFG->wwwrootとは異なる値を使用する必要があります。 One common way to do this is to use 127.0.0.1 for behat, but localhost for standard use. Alternatively you can add an additional hostname in your /etc/hosts file and use this instead. 一般的な方法としてはbehatには127.0.0.1、標準にはlocalhost を使用します。あるいは/etc/hosts ファイルにホスト名を追加して、これを代わりに使用できます。

If you use Docker, then you may be able to use host.docker.internal where your site is hosted on the docker host あなたがDockerを使用している場合、あなたのサイトがホストされているDockerホストhost.docker.internalを使用できます。

Moodle用にBehatを設定する Configure Behat for Moodle

After setting your configuration, you can simply initialise behat: 設定完了後、あなたは単純にBehatを初期化できます:

   $ php admin/tool/behat/cli/init.php

This will install all required Composer dependencies, install a new Moodle site, and put configuration in place これによりComposerに必要なすべての依存関係および新しいMoodleサイトがインストールされます。そして設定が完了します。

When it finishes it will give advice on how to run Behat. 終了後、Behatの使用方法に関するアドバイスが表示されます。

Run Behat tests

Before running behat, ensure that your Selenium server is running. Behatを実行する前に、あなたのSeleniumサーバが起動していることを確認してください。

The easiest way to run behat, and test that everything works is by simply running it using the command provided when you initialised Behat. If you didn't make a note of it, you can just run the initialisation again. Behatのすべての動作を確認する最も簡単な方法はBehat初期化時に提供されるコマンドを使用してBehatを実行してみることです。あなたがBehat初期化時に提供されるコマンドをメモしていない場合、再度初期化を実行してください。

   $ php admin/tool/behat/cli/init.php

This will give you a command which you can then run. This command will run every behat scenario, which will take a considerable amount of time. This command will look a bit like this: これはあなたが実行できるコマンドを提供します。このコマンドはすべてのBehatシナリオを実行するため、かなりの時間を要します。このコマンドは次のようになります:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml

To make this more useful you an combine it with flags, for example to only run certain tags or for a specific Behat Feature file, or Scenario. 例えば特定のタグ、特定のBehatフィーチャファイルまたはシナリオのみを実行できます。

To run all features/scenarios which are tagged with mod_forum: mod_forumでタグ付けされたすべてのフィーチャ/シナリオを実行してください:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --tags=@mod_forum

To run one specific feature file: 特定のフィーチャファイルを実行するには:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml `pwd`/mod/forum/tests/behat/private_replies.feature

To run one specific scenario within a feature file: フィーチャファイル内の特定のシナリオを実行するには:

   # To run the Scenario on line 38 of the file:
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml `pwd`/mod/forum/tests/behat/private_replies.feature:38

To run one specific scenario by name: シナリオ名で特定のシナリオを実行するには:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --name="As a teacher I can see my own response"

See the upstream documentation on Behat, and Gherkin filters for more information. 詳細はBehatおよびGherkinフィルタに関するアップストリームドキュメンテーションをご覧ください。

異なるブラウザで実行する Running using a different browser

The default browser in Behat is Firefox. To specify a different browser profile, you can add the --profile argument. For example, to use Chrome in Headless mode: BehatのデフォルトブラウザはFirefoxです。別のブラウザプロファイルを指定するには--profile引数を追加してください。以下、例えばChromeをヘッドレスモードで使用する場合です:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --profile=headlesschrome

If you are using the moodle-browser-config utility, then you can use any profile listed in moodle-browser-config. Otherwise you can write your own browser profile configuration. あなたがmoodle-browser-configユーティリティを使用している場合、moodle-browser-configに一覧表示されている任意のプロファイルを使用できます。そうでない場合、あなたは独自のブラウザプロファイル設定を記述できます。

高度なテスト Advanced testing

Selenium なしでテストを実行する (chromedriver, geckodriver) Run tests without Selenium (chromedriver, geckodriver)

Historically, Behat required Selenium server, however browsers now make use of their own automation layer. For example, Firefox uses Geckodriver and Chrome uses Chromedriver. As a result the use of Selenium itself is now optional. 歴史的にBehatはSeleniumサーバを必要としていましたが、現在ではブラウザが独自の自動化レイヤを使用しています。例えばFirefoxはGeckodriverを使用します。また、ChromeはChromedriverを使用しています。その結果、Selenium自体の使用は任意となりました。

The moodle-browser-config tool includes standard profiles to use these drivers directly and without the use of Selenium. moodle-browser-configツールはSeleniumを使用せずにこれらのドライバを直接使用するための標準プロファイルを含みます。

To use the drivers directly, you must run the driver itself, for example to run chromedriver: ドライバーを直接使用す場合、あなたはドライバ自体を実行する必要があります。例えばchromedriverを実行します:

   $ chromedriver

To run geckodriver:

   $ geckodriver

Note: geckodriver runs on port 4444 by default. You cannot geckodriver at the same time as selenium. 注意: デフォルトでgeckodriverはポート4444で実行されます。あおなたはseleniumと同時にはgeckodriverを実行できません。

After starting your preferred browser, you can then run behat and specify an alternative profile, for example: あなたの好みのブラウザを起動した後、behatを実行して代替プロファイルを指定できます。例えば以下のようになります:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --profile=geckodriver

ヘッドレスブラウザ Headless browsers

There are a number of reasons that you may prefer to use a headless browser. It can be particularly helpful if you are running the tests on a remote system, for example over SSH, or if you do not want to be interrupted by browsers popping up on your machine. ヘッドレスブラウザの使用を好む理由はいくつかあります。例えばSSH経由でリモートシステム上でテストを実行する場合や、自分のマシンにブラウザがポップアップして邪魔されたくない場合等、特に有用です。

The following headless profiles are some of those provided in the moodle-browser-config tool as standard: 以下のヘッドレスプロファイルはmoodle-browser-configツールで標準的に提供されるプロファイルの一部です:

  1. headlessfirefox Use Firefox via Selenium, without displaying the GUI
  2. headlessfirefox FirefoxをSelenium経由で使用する (GUIの表示なし)
  3. headlessgeckodriver Use Firefox with Geckodriver directly, without displaying the GUI
  4. headlessgeckodriver Firefoxを直接Geckodriverで使用する (GUIの表示なし)
  5. headlesschrome Use Chrome via Selenium, without displaying the GUI
  6. headlesschrome ChromeをSelenium経由で使用する (GUIの表示なし)
  7. headlesschromedriver Use Chrome with Chromedriver directly, without displaying the GUI
  8. headlesschromedriver Chromeを直接Chromedriverで使用する (GUIの表示なし)

These can be provided to the --profile argument to behat:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --profile=headlesschrome

これらはbehatに--profile引数として渡せます:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --profile=headlesschrome

並列処理 Parallel runs

初期設定ではMoodleは1つのMoodleインストレーションですべてのテストが連続して実行されるようBehatを設定します。 これは開発者が単一のテストを実施する場合、または小規模なテストスイートを実施する場合に最適です。しかしこれにはかなり時間を要します。A lot of time is spent waiting in Behat for things to happen. This may be for a page to load, for additional content to load, or even explicit waits because some interactions must be deliberately slowed down. As a result, a system running behat will not have a particularly high load most of the time.Behatでは多くの待ち時間が発生します。これはページがロードされるまでの時間であったり、追加コンテンツがロードされるまでの時間であったり、あるいはいくつかのインタラクションを意図的に遅くしなければならないための明示的な待ち時間であったりします。その結果、ほとんどの場合、behatが動作しているシステムは特に高い負荷を持つことはないでしょう。

If you want to run a large suite of tests then it is possible to take advantage of the relatively low resource consumption by running several behat runners in parallel. This is commonly referred to as a Parallel run. あなたがもし大規模なテスト群を実行したい場合、いくつかのbehatランナを並列に実行することで比較的低いリソース消費量を利用できます。これは一般に「並列実行」(Parallel run) と呼ばれます。

A parallel run of behat takes the same codebase and creates several installations rather than just a single Moodle installation. The behat Feature files are then grouped and allocated between each of the separate installations. Behatの並列実行では同じコードベースを使用した上で、単一のMoodleインストールではなく、複数のインストールを作成します。そして、Behatフィーチャファイルはグループ化され各インストレーションに割り当てられます。

To support this, each of the parallels runs needs its own: これをサポートするため、それぞれの並列実行では自サイトに以下を持つ必要があります:

  • behat_wwwroot
  • behat_dataroot
  • database

Rather than using an entirely separate database, the same database is actually used, but a different behat_prefix is used to prefix the table names in the database differently. 全く別のデータベースを使うのではなく、実際には同じデータベースを使用しますが、異なるbehat_prefix接頭辞でデータベース内のテーブル名を変えています。

Installation

The Behat initialisation command is responsible for preparing Moodle to run a standard run. You'll have used this before when installing for a standard run: Behat初期化コマンドはMoodleが標準実行を実行するため準備します。あなたはこのコマンドを標準実行のためのインストール時に使用したことがあるはずです:

   $ php admin/tool/behat/cli/init.php

The same command can be used to prepare Moodle for a parallel run by specifying the --parallel or -j parameter: 同じコマンドを使用して--parallelまたは-jパラメータを指定することでMoodleの並列実行を準備できます:

   // Below command will initialise moodle to run 3 parallel tests.
   // 以下のコマンドは3件の並列テスト実行のためにmoodleを初期化します。
   $ php admin/tool/behat/cli/init.php --parallel=3

This can be combined with the --add-core-features-to-theme or -a flag to prepare Behat to run with all installed themes. これは--add-core-features-to-themeまたは-aフラグと組み合わせることで、インストールされているすべてのテーマでBehatを動作させることができます。

A number of advanced options are also available but you are unlikely to need these: 高度なオプションも多数用意されていますが、あなたがこれらを必要とすることはまずないでしょう:

  1. -m=<number> or --maxruns=<number> Max parallel site which should be initialised at one time. If your system is slow, then you can initialise sites in chucks.
  2. -m=<number> または --maxruns=<number> 一度に初期化する並列サイトの最大数です。システムが遅い場合、チャックでサイトを初期化できます。
  3. --fromrun=<number> Initialise site to run specified run from. Used for running acceptance tests on different vms
  4. --fromrun=<number> 指定された実行開始日時を初期化します。異なるvmsでの受け入れテストを実行する際に使用します。Initialise site to run specified run from. Used for running acceptance tests on different vms
  5. --torun=<number> Initialise site to run specified run till. Used for running acceptance tests on different vms
  6. --torun=<number> 指定された実行終了日時を初期化します。異なるvmsでの受け入れテストを実行する際に使用します。Initialise site to run specified run till. Used for running acceptance tests on different vms
  7. -o or --optimize-runs This option will split features with specified tags in all parallel runs, so they are executed first when parallel run gets executed.
  8. -oまたは--optimize-runsこのオプションはすべての並列実行で指定されたタグを持つフィーチャを分割して、並列実行時に最初に実行されるようにします。

You can view details of all of these using the --help flag to admin/tool/behat/cli/init.php あなたはadmin/tool/behat/cli/init.php--helpオプションを付けることで、これらの詳細すべてを確認できます。

並列テストを実行する Running Parallel tests

You can use the Moodle behat runner to run all tests, including Standard runs. It is an intelligient wrapper around the standard ./vendor/bin/behat command which specifies the configuration file, and other required features. あなたはMoodle behatランナを使って標準テストを含むすべてのテストを実行できます。これは設定ファイルおよびその他の必要な機能を指定する標準的な./vendor/bin/behatコマンドのインテリジェントなラッパです。

   $ php admin/tool/behat/cli/run.php

Many of the standard options and parameters that can be passed to ./vendor/bin/behat can also be passed to the Moodle runner, for example: ./vendor/bin/behatに渡せる標準オプションおよびパラメータの多くはMoodleランナにも渡せます。例えば以下のようになります:

  1. --tags Run tests which match the specified tags
  2. --tags 指定されたタグに合致するテストを実行します。
  3. --name="Scenario name" Run a test matching the supplied scenario name
  4. --name="シナリオ名" 提供されたシナリオ名に合致するテストを実行します。
  5. --feature="/path/to/test.feature" Run a specific feature file.
  6. --feature="/path/to/test.feature" 特定のフィーチャファイルを実行します。
  7. --suite Features for specified theme will be executed.
  8. --suite 指定したテーマのフィーチャが実行されます。

The runner also includes a number of custom parameters relating to parallel runs: ランナには並列実行に関する多くのカスタムパラメータも含まれます:

  1. --replace Replace args string with run process number, useful for output and reruns.
  2. --replace 引数文字列を実行プロセス番号に置き換えます。
  3. --fromrun Execute run starting from (Used for parallel runs on different vms)
  4. --fromrun 指定した日時から実行を開始します (異なるvmsでの並列実行に使用されます)。
  5. --torun Execute run till (Used for parallel runs on different vms)
  6. --torun 指定した日時まで実行を継続します (異なるvmsでの並列実行に使用されます)。

The --replace feature is particularly useful and can be used to replace a string in the command with the run number. This is useful when specifying output formats, and rerun files as noted below. --replace機能は特に便利でコマンド内の文字列を実行ナンバに置き換えるために使用できます。これは出力形式を指定する場合、および後述の再実行ファイルを指定する場合に便利です。

The following example demonstrates how Behat might be initialised with three parallel runs, to run on all themes: 以下の例ではBehatを3つの並列実行で初期化して、すべてのテーマで実行する方法を示します:

   $ php admin/tool/behat/cli/init.php --parallel=3 --add-core-features-to-theme

And then to run all tests matching the @tool_myplugin tag, against the classic theme: そして、@tool_mypluginタグに合致するすべてのテストをクラシックテーマに対して実行します:

   $ php admin/tool/behat/cli/run.php --tags="@tool_myplugin" --suite="classic"
並列実行用のカスタムパラメータ Custom parameters for parallel runs

You can set following custom config options for parallel runs via $CFG->behat_parallel_run. It's an array of options where 1st array is for 1st run and so on. あなたはCFG->behat_parallel_runで並列実行のための以下のカスタム設定オプションを設定できます。これはオプションの配列です。最初のの配列は1回目の実行用、2番目の配列は2回目の実行用です。

   $CFG->behat_parallel_run = [
       [
          'dbtype' => 'mysqli',
          'dblibrary' => 'native',
          'dbhost' => 'localhost',
          'dbname' => 'moodletest',
          'dbuser' => 'moodle',
          'dbpass' => 'moodle',
          'behat_prefix' => 'mdl_',
          'wd_host' => 'http://127.0.0.1:4444/wd/hub',
          'behat_wwwroot' => 'http://127.0.0.1/moodle',
          'behat_dataroot' => '/home/example/bht_moodledata'
      ],
      // ...
   ],

To set different selenium servers for parallel runs, you can use following. NOTE: Running parallel (headless) runs on different selenium servers avoid random focus failures. 並列実行用に異なるseleniumサーバを設定したい場合、あなたは以下を使用できます。メモ: 異なるseleniumサーバ上での並列 (ヘッドレス) 実行によりランダムなフォーカスの失敗を回避できます。

   $CFG->behat_parallel_run = [
       ['wd_host' => 'http://127.0.0.1:4444/wd/hub'],
       ['wd_host' => 'http://127.0.0.1:4445/wd/hub'],
       ['wd_host' => 'http://127.0.0.1:4446/wd/hub'],
   ];

テストフィルタ Tests filters

With the --tags or the -name Behat options you can filter which tests are going to run or which ones are going to be skipped. There are a few tags that you might be interested in: --tagsまたは-name Behat オプションで実行するテストまたはスキップするテストを絞り込めます。以下、あなたが興味のありそうなタグがいくつかあります:

  • @javascript: All the tests that runs in a browser using Javascript; they require Selenium or the browser's own automation layer, as per Running acceptance test#Run tests without Selenium .28chromedriver.2C geckodriver.29, to be running, otherwise an exception will be thrown.
  • @javascript: Javascriptを使用してブラウザで実行されるすべてのテストには受け入れテストを実行する#Seleniumなしでテストを実行する (chromedriver, geckodriver) にあるようにSeleniumまたはブラウザ独自のレイヤを必要とします。そうでない場合、例外が発生します。
  • @_file_upload: All the tests that involves file uploading or any OS feature that is not 100% part of the browser. They should only be executed when Selenium is running in the same machine where the tests are running.
  • @_file_upload: ファイルアップロードまたはブラウザの一部ではないOSフィーチャを含むすべてのテストです。これらのテストはテストが実行されている同じマシンでSeleniumが実行されているときにのみ実行されるべきです。
  • @_alert: All the tests that involves Javascript dialogs (alerts, confirms...) are using a feature that is OS-dependant and out of the browser scope, so they should be tag appropriately as not all browsers manage them properly.
  • @_alert: Javascriptのダイアログ (警告、確認...) を含むすべてのテストはOSに依存して、ブラウザの範囲外のフィーチャを使用しています。すべてのブラウザが適切に管理しているわけではないため、適切にタグ付けする必要があります。
  • @_switch_window: All the tests that are using the I switch to "NAME" window step should be tagged as not all browsers manage them properly.
  • @_switch_window: 高度なフィーチャであり、すべてのブラウザが適切に管理しているわけではないため、「NAMEフレームに切り替える」ステップを使用しているすべてのテストはタグ付けされるべきです。
  • @_switch_iframe: All the tests that are using the I switch to "NAME" iframe steps should be tagged as it is an advanced feature and some browsers may have problems dealing with them
  • @_cross_browser: All the tests that should run against multiple combinations of browsers + OS in a regular basis. The features that are sensitive to different combinations of OS and browsers should be tagges as @_cross_browser.
  • @componentname: Moodle features uses the Frankenstyle component name to tag the features according to the Moodle subsystem they belong to.

出力フォーマット Output formats

Behat is able to output in a number of different formats, and to different locations as required. Behatは必要に応じて様々なフォーマットで様々な場所に出力できます。

This can be achieved by specifying the --format, and --out parameters when running behat, for example: これは例えばbehatの実行時に--formatおよび--outパラメータを指定して実現できます。例えば次ようになります:

   // Run behat, using the 'pretty' format and outputting the value to /tmp/pretty.txt
   // Behatを実行して、prettyフォーマットで値を/tmp/pretty.txtに出力します。
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml \
       --format=pretty --out=/tmp/pretty.txt

It is also possible to output to multiple formats simultaneously by repeating the arguments, for example: また、引数を繰り返すことで複数のフォーマットに同時に出力できます:

Since Moodle 3.1 option for output is: Moodle 3.1以降、出力オプションは以下のとおりです:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml \
       --format=pretty --out=/tmp/pretty.txt \
       --format=moodle_progress --out=std

The following output formats are supported: 以下の出力フォーマットがサポートされます:

  1. progress: Prints one character per step.
  2. progress: 1ステップにつき1文字を表示します。
  3. pretty: Prints the feature as is.
  4. pretty: 機能をそのまま表示します。
  5. junit: Outputs the failures in JUnit compatible files.
  6. junit: 機能をJUnit互換ファイルに出力します。
  7. moodle_progress: Prints Moodle branch information and dots for each step.
  8. moodle_progress: それぞれのステップでMoodleブランチ情報およびドットを出力します。
  9. moodle_list: List all scenarios.
  10. moodle_list: すべてのシナリオを一覧表示します。
  11. moodle_stepcount: List all features with total steps in each feature file. Used for parallel run.
  12. moodle_stepcount: 各フィーチャファイル内のすべてフィーチャを総ステップ数と共に一覧表示します。並列実行時に使用します。
  13. moodle_screenshot: Take screenshot and core dump of each step. With following options you can dump either or both.
  14. moodle_screenshot: 各ステップのスクリーンショットおよびコアダンプを取得します。あなたは以下のオプションを使用してどちらか一方または両方をダンプできます。
    1. --format-settings '{"formats": "image"}': will dump image only
    2. --format-settings '{"formats": "image"}': はイメージのみダンプします。
    3. --format-settings '{"formats": "html"}': will dump html only.
    4. --format-settings '{"formats": "html"}': はHTMLのみダンプします。
    5. --format-settings '{"formats": "html,image"}': will dump both.
    6. --format-settings '{"formats": "html,image"}': 両方をダンプします。
    7. --format-settings '{"formats": "html", "dir_permissions": "0777"}'

Note: If you want to see the failures immediately (rather than waiting ~3 hours for all the tests to finish) then either use the -v option to output a bit more information, or change the output format using --format. Format 'pretty' (-f pretty) is sufficient for most cases, as it outputs each step outcomes in the command line making easier to see the progress. 注意: すべてのテストが終了するまで最大3時間待つよりも、失敗をすぐに確認したい場合、--v オプションを使用してもう少し多くの情報を出力するか、--formatを使用して出力形式を変更してください。フォーマット「pretty」 (-f pretty) はコマンドラインに各ステップの結果を出力して進捗状況を見やすくするため、ほとんどの場合において十分です。

並列実行時、あなたはそれぞれの実行結果を出力したい場合もあることでしょう。あなたが標準パスを指定できる場合、それぞれの並列実行は他のファイルをオーバーライドします。これは--replaceオプションで処理できます: When working with parallel runs, you may wish to have an output for each run. If you were to specify a standard path for this then each of the parallel runs would overwrite the others file. The --replace option allows this to be handled:

   $ admin/tool/behat/cli/run.php \
       --replace="{runprocess}" \
       --format=pretty --out=/tmp/pretty_{runprocess}.txt \
       --format=moodle_progress --out=std

In this example, the --replace argument is provided with a value of {runprocess}. Anywhere that {runprocess} appears in the command it will be replaced with the run number. The above command will generate a set of commands like: この例では--replace引数に{runprocess}という値を指定しています。{runprocess}がコマンドのどこに出てきた場合でも、実行番号に置き換えられます。上のコマンドは以下のようなコマンド群を生成します:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun1/behat/behat.yml \
       --format=pretty --out=/tmp/pretty_1.txt \
       --format=moodle_progress --out=std
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun2/behat/behat.yml \
       --format=pretty --out=/tmp/pretty_2.txt \
       --format=moodle_progress --out=std
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun3/behat/behat.yml \
       --format=pretty --out=/tmp/pretty_3.txt \
       --format=moodle_progress --out=std

失敗シナリオの再実行 Rerun failed scenarios

With slow systems or parallel run you may experience see some random failures. These may happen when your system is too slow, when it is too fast, or where a page depends on external dependencies. 低速のシステムまたは並列実行では不規則な障害が発生することがあります。これはシステムが遅すぎる場合、速すぎる場合、あるいはページが外部依存関係に依存している場合に発生する可能性があります。

To help with this it is possible to rerun any failed scenarios using the --rerun option to Behat. Behatの--rerunオプションを使用することにより、失敗したシナリオを再実行できます。

The following example runs Behat with the rerun option: 以下の例ではBehatをrerunオプション付きで実行します:

   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml \
       --format=pretty --out=/tmp/pretty.txt \
       --format=moodle_progress --out=std \
       --rerun

If any single test fails then the command will return a non-zero exit code. Running the same command again will mean that only failed scenarios are run. テストが1つでも失敗した場合、コマンドはゼロ以外の終了コードを返します。同じコマンドを再度実行した場合、失敗したシナリオのみが実行されることになります。。

For a parallel run it can be called as follows:

   $ admin/tool/behat/cli/run.php \
       --replace="{runprocess}" \
       --format=pretty --out=/tmp/pretty_{runprocess}.txt \
       --format=moodle_progress --out=std \
       --rerun

The Moodle behat runner also includes an --auto-rerun option which will automatically rerun failed scenarios exactly once. Moodle behatランナには--auto-rerunオプションも含まれているため、失敗したシナリオを自動的に一度だけ再実行できます。

特定のテーマでbehatを実行する Running behat with specified theme

Behat can be run with any installed theme, but it must be initialised with the -a or --add-core-features-to-theme option first. Behatはインストールされているテーマで実行できますが、最初に-aまたは--add-core-features-to-themeオプションで初期化する必要があります。

After configuring the theme can be specified using the --suite parameter.

Note: The default theme in Moodle (boost) has the suite name default.

   // Initialise Behat for all themes:
   $ php admin/tool/behat/cli/init.php --add-core-features-to-theme
   
   // Run Behat against all initalised themes:
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml
   
   // Run Behat against just the default theme when all themes were initialised:
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --suite=default
   
   // Run Behat against just the 'classic' theme when all themes were initialised:
   $ vendor/bin/behat --config /Users/nicols/Sites/moodles/sm/moodledata_behat/behatrun/behat/behat.yml --suite=classic

seleniumサーバの開始にDockerを使用する Using Docker to start selenium server

There are a wide range of docker images available which contain a browser with Selenium. You will probably be using the official SeleniumHQ images unless you have a specific reason not to.

The complete list of available SeleniumHQ images is available at https://hub.docker.com/u/selenium/.

Moodle uses the standalone version and any recent version with version 3.141.59 or higher is supported.

For any test which uploads files, for example when interacting with the filepicker, you must also ensure that the Moodle directory is mounted as on your local filesystem.

An example usage is:

   $ docker run -d -p 4444:4444 -v `pwd`:`pwd` selenium/standalone-firefox:latest

Change config.php file

In the Moodle config.php file you must change the $CFG->behat_wwwroot to an address that can be reached from within the docker image.

You cannot use localhost or 127.0.0.1 as this will be the IP address of the docker container itself.

On some more recent versions of Docker you can use http://host.docker.internal/, for example if my site is located at /sm on my development machine, I can set the following:

   $CFG->behat_wwwroot = 'http://host.docker.internal/sm';


Manually configuring other browsers =

If you would prefer not to use the moodle-browser-config tool but still wish to specify different browsers then you can do so using the $CFG->behat_profiles array. Each key/value pair contains a profile name, and the configuration for that profile. For example:

   $CFG->behat_profiles = [
       'chrome' => [
           'browser' => 'chrome',
           'tags' => '@javascript',
       ],
   ];

More info about alternative browsers

Troubleshooting

Increasing timeouts

You may see errors such as:

Javascript code and/or AJAX requests are not ready after 10 seconds.
There is a Javascript error or the code is extremely slow.

Sometimes this indicates a genuine problem with the code, but if you are using a slow computer, it may just mean that your browser did not finish generating parts of the UI before behat tried was finished.

If you find that this happens regularly on different scenarios then you may want to increase the timeout:

   $CFG->behat_increasetimeout = 2;

This will increase all the timeouts by a factor of 2; if that isn't sufficient, you could use 3.

Increasing timeouts will make tests run a bit slower (because there are points where Behat waits up to a timeout to make sure something doesn't happen) so don't do this unless you need to.

Note: This is usually an indicator that your development machine is not well tuned. A better option would be to find out where the bottleneck is. This is usually the database configuration.


New step definitions or features are not executed

If you are adding new tests or steps definitions update the tests list

php admin/tool/behat/cli/util.php --enable

For parallel runs, all options for initialising parallel runs are valid

Tests are failing

If you followed all the steps and you receive an unknown weird error probably your browser version is not compatible with the Selenium version you are running. Please refer Working combinations to ensure you have correct Acceptance_testing/Browsers#Working_combinations_of_OS.2BBrowser.2Bselenium of them to run acceptance test.

The tests are failing, and the error message is completely useless

For example, it just says "Error writing to database" with no stack trace.

Add -vv command-line option to get very verbose output.

Errors during setup (before test are launched)

Typical errors are:

  • Behat requirement not satisfied: http://127.0.0.1/m/stable_master is not available, ensure you specified correct url and that the server is set up and started.
  • init.php or util.php complain that "Unknown error 1 This is not a behat test site!". Delete the behat wwwroot (look in config.php for $CFG->behat_dataroot) and drop the behat DB tables (look in config.php for $CFG->behat_prefix). Then try again.
  • Behat is configured but not enabled on this test site.

In order to fix those errors please check that: the behat_dataroot has correct write permissions and that the $CFG->behat* variables are placed before the lib/setup.php include:

require_once(__DIR__ . '/lib/setup.php');

Selenium server is not running

Chrome specific

If you are using chrome, you need to ensure that the driver matches the version of the installed chrome browser – which may change on OS updates/upgrades. Moodle or Selenium will not give the appropriate message – see [https://tracker.moodle.org/browse/MDL-67659 MDL-67659]. One solution is the one suggested in the issue and use Andrew Nicols’ Chromedriver Wrapper which will ensure you have the appropriate driver before running the tests.


External links


Examples

Quick setup and testing using moodle-docker

This is a quick guide to help locally pass tests for your developments, before submitting them:

  1. Set up a default Moodle install using moodle-docker, with the database and Moodle version of your choice. See its README for more details. This will start some docker containers.
  2. Initialize behat to start testing with this command, from the webserver container:
    php admin/tool/behat/cli/init.php
    
  3. Run the behat test of your choice, from the webserver container. For instance:
    vendor/bin/behat --config /var/www/behatdata/behatrun/behat/behat.yml --tags tool_task
    

And you'll see something like:

Moodle 4.0dev (Build: 20210507), 0b47ea0a44a092f9000729ca7b15fff23111538b
Php: 7.3.26, mysqli: 5.7.21, OS: Linux 5.4.0-66-generic x86_64
Run optional tests:

Accessibility: No
Server OS "Linux", Browser: "firefox"
Started at 09-05-2021, 06:00
...................................................................... 70
...............................
12 scenarios (12 passed)
101 steps (101 passed)
2m53.27s (47.61Mb)

See also


Information to re-home

Override behat core context for theme suite

Note: This documentation needs to be rehomed to a location related to writing Behat tests.

To override behat step definitions so as to run behat with specified theme, you should create a contexts within /theme/{MYTHEME}/tests/behat/ with prefix behat_theme_{MYTHEME}_ and suffixed with the context being overridden. For example, if you want to override behat_mod_forum context, then you should create a class /theme/{MYTHEME}/tests/behat/mod_forum/behat_theme_{MYTHEME}_behat_mod_forum.php

Disable behat context or features to run in theme suite

Note: This documentation needs to be rehomed to a location related to writing Behat tests.

To disable specific contexts and features from being executed by a specific theme/suite you can create a /theme/{MYTHEME}/tests/behat/blacklist.json file with following format.

{
  "contexts": [
    "behat_grade",
    "behat_navigation",
  ],
  "features": [
    "auth/tests/behat/login.feature",
    "grade/tests/behat/grade_hidden_items.feature",
   ]
}

The above will:

  1. disable the use of step_definitions from behat_grade and behat_navigation while running theme suite; and
  2. disable running of scenarios in auth/tests/behat/login.feature and grade/tests/behat/grade_hidden_items.feature.

Override core behat selectors

Note: This documentation needs to be rehomed to a location related to writing Behat tests.

To override behat selectors in specific theme, you should create a class behat_theme_{MYTHEME}_behat_selectors in /theme/{MYTHEME}/tests/behat/behat_theme_{MYTHEME}_behat_selectors.php extending behat_selectors.

Write new tests and behat methods

Note: This documentation needs to be rehomed to a location related to writing Behat tests.

If you want to write tests for your own integration, you can do so by creating new tests with format .feature. Follow instructions in this page to write new tests.

It is also possible to add new steps the moodle behat integration. In order to do so, you will have to create a new .php class with the prefix behat_. Copy the format from lib\behat\behat_base.php, but set your class to extend the behat_base class instead of the MinkExtension. You can define new behat steps by declaring functions with the appropriate heading.

You will not be able to use these steps and features right away. Check this section for instructions on how to update the behat integration.


For further information on how to create new steps definitions, check Acceptance testing/Custom acceptance steps.