「PHPUnit」の版間の差分

提供:MoodleDocs
移動先:案内検索
編集の要約なし
編集の要約なし
 
(同じ利用者による、間の38版が非表示)
1行目: 1行目:
作成中です - [[利用者:Mitsuhiro Yoshida|Mitsuhiro Yoshida]] ([[利用者・トーク:Mitsuhiro Yoshida|トーク]])
=PHPUnitとは=
=PHPUnitとは=
Sebastian BergmannによるPHPUnit はPHP用の高度な単体テストフレームワークです。このフレームワークはComposerの依存ファイルとしてインストールされ、Moodleのインストールには含まれません。PHPUnitテストを実行するにはあなたの開発用コンピュータまたはテストサーバに手動でPHPUnitをインストールする必要があります。
Sebastian BergmannによるPHPUnit はPHP用の高度な単体テストフレームワークです。このフレームワークはComposerの依存ファイルとしてインストールされ、Moodleのインストールには含まれません。PHPUnitテストを実行するにはあなたの開発用コンピュータまたはテストサーバにPHPUnitを手動でインストールする必要があります。


以下の素晴らしいガイドをお読みください:
以下の素晴らしいガイドをお読みください:
* [https://phpunit.de/documentation.html PHPUnitマニュアル]
* [https://phpunit.de/documentation.html PHPUnitマニュアル]
=composer経由のPHPUnitインストール=
=composer経由のPHPUnitインストール=
* Install Composer
* Composerをインストールする
* composerをインストールする
すべてのプラットフォームへのcomposerのインストールに関するインストラクションは次をご覧ください:  https://getcomposer.org/download/
すべてのプラットフォームへのcomposerインストールに関するインストラクションは次をお読みください:  https://getcomposer.org/download/


あなたのmoodleフォルダにcomposerr.pharファイルをインストールしてください。
あなたのMoodleフォルダにcomposerr.pharファイルをインストールしてください。
* Composerインストーラを実行してください。
* Composerインストーラを実行してください。
  cd /your/moodle/dirroot
  cd /your/moodle/dirroot
  php composer.phar install
  php composer.phar install
(接続エラーが発生した場合、以下のコマンドをお試しください ... )
(接続エラーが発生した場合、以下のコマンドをお試しください ... )
  php composer.phar install --prefer-source
  php composer.phar install --prefer-source
トラブルシューティング:
トラブルシューティング:
* あなたがWindowsでプロキシを利用している場合、HTTP_PROXY環境変数にHTTPプロキシのアドレスおよびポートを設定することでComposerが正しくファイルをダウンロードできるようになります。
* あなたがWindowsでプロクシを使用している場合、HTTP_PROXY環境変数にHTTPプロクシのアドレスおよびポートを設定することでComposerがファイルを正しくダウンロードできるようになります。
* あなたはcomposeのインストール時にgithubの認証情報の入力を求められる場合があります。
* あなたはcomposeインストール時にgithubの認証情報の入力を求められる場合があります。
** これはgithubによるレート制限を回避するための個人用アクセストークンの生成に使用されます。
** これはgithubによるレート制限を回避するための個人用アクセストークンの生成に使用されます。
** あなたがgithubアカウントで2要素認証を有効にしている場合、または自分で認証情報を提供したくない場合、手動でトークンを生成する必要があります:
** あなたがgithubアカウントで2要素認証を有効にしている場合、または自分で認証情報を提供したくない場合、手動でトークンを生成する必要があります:
*** https://github.com/settings/applications にアクセスしてパーソナルアクセストークンをリクエストしてください。
*** https://github.com/settings/applications にアクセスしてパーソナルアクセストークンをリクエストしてください。
*** このトークンをあなたの[https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]ファイルにコピーしてください。
*** このトークンをあなたの[https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]ファイルにコピーしてください。
** ( このバグに関する詳細は [https://github.com/composer/composer/issues/2280 composer issue #2280] をご覧ください。)
** ( このバグに関する詳細は [https://github.com/composer/composer/issues/2280 composer issue #2280] をご覧ください)
 


詳細インストラクション:
詳細インストラクション:
34行目: 29行目:
* [[OS XでのPHPUnitインストレーション]]
* [[OS XでのPHPUnitインストレーション]]
== 前のPEARベースバージョンをアンインストールする ==
== 前のPEARベースバージョンをアンインストールする ==
composerを使用する前、このページではPEAR経由でのphpunitインストールを推奨していました。あなたがもしそうしていたのでしたら、今すぐそのパッケージをアンインストールした方がよいでしょう。以下のコマンドで:アンインストールできるはずです:
composerを使用する前、このページではPEAR経由でのphpunitインストールを推奨していました。あなたがもしそうしていたのでしたら、今すぐそのパッケージをアンインストールした方がよいでしょう。以下のコマンドでアンインストールできるはずです:
   $ pear uninstall phpunit/DbUnit
   $ pear uninstall phpunit/DbUnit
   $ pear uninstall phpunit/PHPUnit
   $ pear uninstall phpunit/PHPUnit
70行目: 65行目:
|}
|}
=テスト環境のインストレーション=
=テスト環境のインストレーション=
PHPUnitの統合には専用のデータベースおよびdatarootが必要です。まず最初にconfig.phpに新しい dataroot ディレクトリおよび接頭辞を追加してください。config-dist.phpに例があります (「セクション 9」までスクロールダウンしてください)。
PHPUnitの統合には専用のデータベースおよびdatarootが必要です。まず最初にconfig.phpに新しい datarootディレクトリおよび接頭辞を追加してください。config-dist.phpに例があります (「セクション 9」までスクロールダウンしてください)。
  $CFG->phpunit_prefix = 'phpu_';
  $CFG->phpunit_prefix = 'phpu_';
  $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
  $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
PHPUnitのテストの中にはインターネットへのライブ接続を必要とするものがあります。システムがインターネットに直接接続されていない場合、config.phpでプロキシを指定する必要があります - 通常は管理設定ユーザインターフェイスで指定します (あなたがプロクシを使用していない場合、このステップは省略してください)。正しい値がわからない場合は、関連する管理者設定ページまたはデータベースのmdl_configテーブルで設定を確認してください。
PHPUnitのテストの中にはインターネットへのライブ接続を必要とするものがあります。システムがインターネットに直接接続されていない場合、config.phpでプロキシを指定する必要があります - 通常は管理設定ユーザインターフェイスで指定します (あなたがプロクシを使用していない場合、このステップは省略してください)。正しい値がわからない場合、関連する管理者設定ページまたはデータベースのmdl_configテーブルで設定を確認してください。


  //  通常のプロキシ設定 Normal proxy settings
  //  通常のプロキシ設定
  $CFG->proxyhost = 'wwwcache.example.org';
  $CFG->proxyhost = 'wwwcache.example.org';
  $CFG->proxyport = 80;
  $CFG->proxyport = 80;
  $CFG->proxytype = 'HTTP';
  $CFG->proxytype = 'HTTP';
  $CFG->proxybypass = 'localhost, 127.0.0.1, .example.org';
  $CFG->proxybypass = 'localhost, 127.0.0.1, .example.org';
  // Omit the next lines if your proxy doesn't need a username/password:
  // プロキシがユーザ名/パスワードを必要としない場合は次の行を省略してください:
  $CFG->proxyuser = 'systemusername';
  $CFG->proxyuser = 'systemusername';
  $CFG->proxypassword = 'systempassword';
  $CFG->proxypassword = 'systempassword';
Moodle 2.8.5以降、単体テストのために特定のデータベース設定を提供することもできます (提供されない場合、標準のデータベース設定が使用されます):
Moodle 2.8.5以降、単体テストのために特定のデータベース設定を提供することもできます (提供されない場合、標準のデータベース設定が使用されます):
<pre>$CFG->phpunit_dbtype    = 'pgsql';      // 'pgsql', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' or 'oci'
<pre>$CFG->phpunit_dbtype    = 'pgsql';      // 'pgsql', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' または 'oci'
$CFG->phpunit_dblibrary = 'native';    // 'native' only at the moment
$CFG->phpunit_dblibrary = 'native';    // 現在、'native' のみ
$CFG->phpunit_dbhost    = '127.0.0.1';  // eg 'localhost' or 'db.isp.com' or IP
$CFG->phpunit_dbhost    = '127.0.0.1';  // 'localhost' または 'db.isp.com' または IP
$CFG->phpunit_dbname    = 'mytestdb';    // database name, eg moodle
$CFG->phpunit_dbname    = 'mytestdb';    // データベース名 例) moodle
$CFG->phpunit_dbuser    = 'postgres';  // your database username
$CFG->phpunit_dbuser    = 'postgres';  // あなたのデータベースユーザ名
$CFG->phpunit_dbpass    = 'some_password';  // your database password</pre>
$CFG->phpunit_dbpass    = 'some_password';  // あなたのデータベースパスワード</pre>
Then you need to initialise the test environment using following command.
そして、あなたは以下のコマンドでテスト環境を初期化する必要があります。
  cd /home/example/moodle
  cd /home/example/moodle
  php admin/tool/phpunit/cli/init.php
  php admin/tool/phpunit/cli/init.php
このコマンドはアップグレード、プラグインのインストールおよびアンインストール、開発中のプラグインへのテスト追加等の場合に繰り返し実行する必要があります:
このコマンドはアップグレード、プラグインのインストールおよびアンインストール、開発中のプラグインへのテスト追加等の場合に繰り返し実行する必要があります。


'''NOTE:''' make sure that your php cli executable (or the one you want to use) is correctly on your path as the individual init scripts will call it repeatedly. Also, ensure en_AU locale is installed on your server.
'''注意:''' 個々のinitスクリプトが繰り返し呼び出すため、php cliの実行ファイル (またはあなたが使用したいもの) が正しくパス上にあることを確認してください。また、en_AUロケールがサーバにインストールされていることを確認してください。
'''注意:''' 個々のinitスクリプトが繰り返し呼び出すため、php cliの実行ファイル (またはあなたが使用したいもの) が正しくパス上にあることを確認してください。また、en_AU ロケールがサーバにインストールされていることを確認してください。
== LDAP ==
== LDAP ==
If you want to run LDAP unit tests you must have a working, configured LDAP environment on your test server. There must be a basic LDAP tree structure in place or tests will fail with "Search: No such object". Build an LDAP tree structure in a new shell prompt:
あなたがLDAP単体テストを実行したい場合、テストサーバ上で動作する設定済みのLDAP環境を準備する必要があります。基本的なLDAPツリー構造がない場合、「Search: No such object」 とメッセージが表示されてテストが失敗します。新しいシェルプロンプトでLDAPツリー構造を構築してください:
<pre>
<pre>
$ ldapadd -H ldap://127.0.0.1 -D "cn=admin,dc=yourcomputer,dc=local" -W
$ ldapadd -H ldap://127.0.0.1 -D "cn=admin,dc=yourcomputer,dc=local" -W
107行目: 101行目:
ou:YOURCOMPUTER
ou:YOURCOMPUTER
</pre>
</pre>
In config.php tell Moodle where to look for your test LDAP environment:
あなたのテスト用LDAP環境を探す場所をconfig.phpでMoodleに教えてください:
<pre>
<pre>
// Constants for auth/ldap tests.
// auth/ldapテスト用の定数
define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_AUTH_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_AUTH_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
115行目: 109行目:
define('TEST_AUTH_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
define('TEST_AUTH_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');


// Constants for enrol/ldap tests.
// enrol/ldapテスト用定数
define('TEST_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_ENROL_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_ENROL_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
121行目: 115行目:
define('TEST_ENROL_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
define('TEST_ENROL_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');


// Constants for lib/ldap tests.
// lib/ldapのテスト用定数
define('TEST_LDAPLIB_HOST_URL', 'ldap://127.0.0.1');
define('TEST_LDAPLIB_HOST_URL', 'ldap://127.0.0.1');
define('TEST_LDAPLIB_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_LDAPLIB_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
127行目: 121行目:
define('TEST_LDAPLIB_DOMAIN', 'dc=yourcomputer,dc=local');
define('TEST_LDAPLIB_DOMAIN', 'dc=yourcomputer,dc=local');
</pre>
</pre>
=Test execution=
=テスト実行=
To execute all test suites from main configuration file execute the <syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight> script from your <syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight> directory.
すべてのテストスイートをメイン設定ファイルから実行したい場合、あなたの<syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight>スクリプト<syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight>をディレクトリで実行してください。
  cd /home/example/moodle
  cd /home/example/moodle
  vendor/bin/phpunit
  vendor/bin/phpunit
The rest of examples uses <syntaxhighlight lang="php">phpunit</syntaxhighlight>, please substitute it with <syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight> or create a shortcut in your dirroot.
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to <syntaxhighlight lang="php">phpunit.xml</syntaxhighlight> from your <syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight>.
残りの例では<syntaxhighlight lang="php">phpunit</syntaxhighlight>を使用していますが、<syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight>と置換するか、あなたはディレクトリにショートカットを作成する必要があります。
 
あなたはIDE でPHPUnit の設定ファイルへのパスを指定する必要がある場合があります。あなたの<syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight>から<syntaxhighlight lang="php">phpunit.xml</syntaxhighlight>への絶対パスを使用してください。
 
There is an alternative script for running of tests via web interface: <syntaxhighlight lang="php">admin/tool/phpunit/webrunner.php</syntaxhighlight>. Use this as the last resort only when you cannot use the command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.
 
 
===How to run only some tests===
==== Running a single test quickly (PHPUnit 9) ====
{{Moodle 3.11}}
The fastest way to run a single test in PHPUnit 9.5 and higher (Moodle 3.11 and higher) is to use the filter argument:
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter tool_dataprivacy_metadata_registry_testcase
</syntaxhighlight>
To run all tests provided by the single component, use suite and the name it has in the phpunit.xml file. Example:
<syntaxhighlight lang="bash">
vendor/bin/phpunit --testsuite workshopform_accumulative_testsuite
</syntaxhighlight>
Alternatively if you have config files built for each component:
<syntaxhighlight lang="bash">
vendor/bin/phpunit -c mod/workshop/form/accumulative/
</syntaxhighlight>
==== Running a single test quickly (PHPUnit 8) ====
{{Moodle 3.10}}
The fastest way to run a single test in PHPUnit 8.5 and lower (Moodle 3.10 and lower):
<syntaxhighlight lang="bash">
vendor/bin/phpunit my/tests/filename.php
</syntaxhighlight>
so, run this command in the CLI to see a real test in action:
<syntaxhighlight lang="bash">
vendor/bin/phpunit cohort/tests/cohortlib_test.php
</syntaxhighlight>
You can also run a single test method inside a class:
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter test_function_name path/to/file.php
</syntaxhighlight>
Note: You should be careful because it may be possible to run tests this way which are not included in the normal run if, for example, the file is not placed in the correct location. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.
 
Filters can also be applied to capture a group of similar tests across all testsuites:
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter test_flag_user
</syntaxhighlight>
It is also possible to run all tests in a component (subsystem or plugin) by using the testsuite option:
<syntaxhighlight lang="bash">
vendor/bin/phpunit --testsuite mod_forum_testsuite
vendor/bin/phpunit --testsuite core_privacy_testsuite
vendor/bin/phpunit --testsuite core_privacy_testsuite --filter test_component_is_compliant
</syntaxhighlight>
==== Using the @group annotation ====
If you add annotations like
<syntaxhighlight lang="php">
namespace qtype_stack;
/**
* Unit tests for {@link stack_cas_keyval} @ qtype/stack/tests/cas_keyval_test.php.
* @group qtype_stack
*/
class cas_keyval_test extends \basic_testcase {
</syntaxhighlight>
to all the classes in your plugin, then you can run just the tests for your plugin by doing
phpunit --group qtype_stack
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.
==== Using multiple phpunit.xml files ====
It's easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with
vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml
Also, for commodity, you can use this command:
php admin/tool/phpunit/cli/util.php --buildcomponentconfigs
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like
vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // Note that it's not needed to specify the name of the file (if it is 'phpunit.xml').
vendor/bin/phpunit -c question
vendor/bin/phpunit -c question/type/calculated
vendor/bin/phpunit -c backup
vendor/bin/phpunit -c lib/dml
...
or, also
cd directory/with/phpunit.xml
phpunit
=External test resources=
{{Moodle 2.6}}
By default Moodle phpunit tests contact http://download.moodle.org server when testing curl related functionality. Optionally you may checkout a local copy of the test scripts and access it instead:
# clone https://github.com/moodlehq/moodle-exttests to web directory
# add to your config.php or modify phpunit.xml file <syntaxhighlight lang="php">define('TEST_EXTERNAL_FILES_HTTP_URL', 'http://localhost/moodle-exttests');</syntaxhighlight>
=Writing new tests=
* read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation]
* see [[Writing PHPUnit tests]]
=Conversion of existing SimpleTests=
* see [[SimpleTest conversion]]
=PHPUnit support in IDEs=
* [[Setting up Eclipse]]
* [[Setting up Netbeans]]
* [[Setting up PhpStorm]]
=Common Unit Test Problems=
[[Common unit test problems]]
=Performance=
A typical run of full PHPUnit tests for Moodle 2.7 takes 10-20 minutes depending on the machine. If tests run slowly for you:
* Ensure you are using a database and filesystem running on the same machine that is running the tests (not on a remote server).
* Apply developer-only performance settings to your database: [[Postgres Tuning For Developers]]
=Command line tips=
* Run all tests for a plugin (mod_mymodule in this example): vendor/bin/phpunit --testsuite=mod_mymodule_testsuite
* Run only named test: vendor/bin/phpunit --filter=test_my_test_function_name
* Display each test name before running it (useful e.g. if it crashes before the end and you want to know which test it was running at that point) vendor/bin/phpunit --debug (you will probably want to redirect this to a file as it gets very long).
[[Category:Unit testing]]
 
[[en:PHPUnit]]
作成中です - [[利用者:Mitsuhiro Yoshida|Mitsuhiro Yoshida]] ([[利用者・トーク:Mitsuhiro Yoshida|トーク]])
 
=PHPUnitとは=
Sebastian BergmannによるPHPUnit はPHP用の高度な単体テストフレームワークです。このフレームワークはComposerの依存ファイルとしてインストールされ、Moodleのインストールには含まれません。PHPUnitテストを実行するにはあなたの開発用コンピュータまたはテストサーバに手動でPHPUnitをインストールする必要があります。
 
以下の素晴らしいガイドをお読みください:
* [https://phpunit.de/documentation.html PHPUnitマニュアル]
=composer経由のPHPUnitインストール=
* Install Composer
* composerをインストールする
すべてのプラットフォームへのcomposerインストールに関するインストラクションは次をお読みください:  https://getcomposer.org/download/
 
あなたのmoodleフォルダにcomposerr.pharファイルをインストールしてください。
* Composerインストーラを実行してください。
cd /your/moodle/dirroot
 
php composer.phar install
(接続エラーが発生した場合、以下のコマンドをお試しください ... )
php composer.phar install --prefer-source
トラブルシューティング:
* あなたがWindowsでプロキシを利用している場合、HTTP_PROXY環境変数にHTTPプロキシのアドレスおよびポートを設定することでComposerが正しくファイルをダウンロードできるようになります。
* あなたはcomposeのインストール時にgithubの認証情報の入力を求められる場合があります。
** これはgithubによるレート制限を回避するための個人用アクセストークンの生成に使用されます。
** あなたがgithubアカウントで2要素認証を有効にしている場合、または自分で認証情報を提供したくない場合、手動でトークンを生成する必要があります:
*** https://github.com/settings/applications にアクセスしてパーソナルアクセストークンをリクエストしてください。
*** このトークンをあなたの[https://gist.github.com/andrewnicols/c5377ed25a9df1006ce1 ~/.composer/config.json]ファイルにコピーしてください。
** ( このバグに関する詳細は [https://github.com/composer/composer/issues/2280 composer issue #2280] をご覧ください。)
 
 
詳細インストラクション:
* [http://getcomposer.org/doc/00-intro.md Composerドキュメンテーション]
詳細インストラクション:
* [[WindowsでのPHPUnitインストレーション]]
* [[OS XでのPHPUnitインストレーション]]
== 前のPEARベースバージョンをアンインストールする ==
composerを使用する前、このページではPEAR経由でのphpunitインストールを推奨していました。あなたがもしそうしていたのでしたら、今すぐそのパッケージをアンインストールした方がよいでしょう。以下のコマンドで:アンインストールできるはずです:
  $ pear uninstall phpunit/DbUnit
  $ pear uninstall phpunit/PHPUnit
= PHPUnitバージョン =
以下の表はデフォルトのコンポーザ設定を使用した場合、どのMoodleバージョンでどのPHPUnitがインストールされるか示しています。
{| class="wikitable" border="1"
|-
! Moodleバージョン
! PHPUnitバージョン
!リンク
|-
| Moodle 3.11
| PHPUnit 9.5
|[https://phpunit.readthedocs.io/en/9.5/ ドキュメンテーション]
|-
| Moodle 3.10
| PHPUnit 8.5
|[https://phpunit.readthedocs.io/en/8.5/ ドキュメンテーション]
|-
| Moodle 3.7 - 3.9
| PHPUnit 7.5
|[https://phpunit.readthedocs.io/en/7.5/ ドキュメンテーション]
|-
| Moodle 3.4 - 3.6
| PHPUnit 6.5
|[https://phpunit.de/manual/6.5/en/ ドキュメンテーション]
|-
| Moodle 3.2 - 3.3
| PHPUnit 5.5
|[https://phpunit.de/manual/5.5/en/ ドキュメンテーション]
|-
| Moodle 3.1
| PHPUnit 4.8.27
|[https://phpunit.de/manual/4.8/en/ ドキュメンテーション]
|}
=テスト環境のインストレーション=
PHPUnitの統合には専用のデータベースおよびdatarootが必要です。まず最初にconfig.phpに新しい dataroot ディレクトリおよび接頭辞を追加してください。config-dist.phpに例があります (「セクション 9」までスクロールダウンしてください)。
$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
PHPUnitのテストの中にはインターネットへのライブ接続を必要とするものがあります。システムがインターネットに直接接続されていない場合、config.phpでプロキシを指定する必要があります - 通常は管理設定ユーザインターフェイスで指定します (あなたがプロクシを使用していない場合、このステップは省略してください)。正しい値がわからない場合は、関連する管理者設定ページまたはデータベースのmdl_configテーブルで設定を確認してください。
 
//  通常のプロキシ設定 Normal proxy settings
$CFG->proxyhost = 'wwwcache.example.org';
$CFG->proxyport = 80;
$CFG->proxytype = 'HTTP';
$CFG->proxybypass = 'localhost, 127.0.0.1, .example.org';
// Omit the next lines if your proxy doesn't need a username/password:
$CFG->proxyuser = 'systemusername';
$CFG->proxypassword = 'systempassword';
Moodle 2.8.5以降、単体テストのために特定のデータベース設定を提供することもできます (提供されない場合、標準のデータベース設定が使用されます):
<pre>$CFG->phpunit_dbtype    = 'pgsql';      // 'pgsql', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' or 'oci'
$CFG->phpunit_dblibrary = 'native';    // 'native' only at the moment
$CFG->phpunit_dbhost    = '127.0.0.1';  // eg 'localhost' or 'db.isp.com' or IP
$CFG->phpunit_dbname    = 'mytestdb';    // database name, eg moodle
$CFG->phpunit_dbuser    = 'postgres';  // your database username
$CFG->phpunit_dbpass    = 'some_password';  // your database password</pre>
Then you need to initialise the test environment using following command.
cd /home/example/moodle
php admin/tool/phpunit/cli/init.php
このコマンドはアップグレード、プラグインのインストールおよびアンインストール、開発中のプラグインへのテスト追加等の場合に繰り返し実行する必要があります:
 
'''NOTE:''' make sure that your php cli executable (or the one you want to use) is correctly on your path as the individual init scripts will call it repeatedly. Also, ensure en_AU locale is installed on your server.
'''注意:''' 個々のinitスクリプトが繰り返し呼び出すため、php cliの実行ファイル (またはあなたが使用したいもの) が正しくパス上にあることを確認してください。また、en_AU ロケールがサーバにインストールされていることを確認してください。
== LDAP ==
If you want to run LDAP unit tests you must have a working, configured LDAP environment on your test server. There must be a basic LDAP tree structure in place or tests will fail with "Search: No such object". Build an LDAP tree structure in a new shell prompt:
<pre>
$ ldapadd -H ldap://127.0.0.1 -D "cn=admin,dc=yourcomputer,dc=local" -W
dn:dc=yourcomputer,dc=local
objectClass:dcObject
objectClass:organizationalUnit
dc:yourcomputer
ou:YOURCOMPUTER
</pre>
In config.php tell Moodle where to look for your test LDAP environment:
<pre>
// Constants for auth/ldap tests.
define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_AUTH_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_AUTH_LDAP_BIND_PW', '*');
define('TEST_AUTH_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
 
// Constants for enrol/ldap tests.
define('TEST_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_ENROL_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_ENROL_LDAP_BIND_PW', '*');
define('TEST_ENROL_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');
 
// Constants for lib/ldap tests.
define('TEST_LDAPLIB_HOST_URL', 'ldap://127.0.0.1');
define('TEST_LDAPLIB_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_LDAPLIB_BIND_PW', '*');
define('TEST_LDAPLIB_DOMAIN', 'dc=yourcomputer,dc=local');
</pre>
=Test execution=
To execute all test suites from main configuration file execute the <syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight> script from your <syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight> directory.
cd /home/example/moodle
vendor/bin/phpunit
The rest of examples uses <syntaxhighlight lang="php">phpunit</syntaxhighlight>, please substitute it with <syntaxhighlight lang="php">vendor/bin/phpunit</syntaxhighlight> or create a shortcut in your dirroot.
In IDEs, you may need to specify the path to the PHPUnit configuration file. Use the absolute path to <syntaxhighlight lang="php">phpunit.xml</syntaxhighlight> from your <syntaxhighlight lang="php">$CFG->dirroot</syntaxhighlight>.
 
 
There is an alternative script for running of tests via web interface: <syntaxhighlight lang="php">admin/tool/phpunit/webrunner.php</syntaxhighlight>. Use this as the last resort only when you cannot use the command-line interface on your test server. It will most probably break due to permissions problems if you try to execute it both from command-line and from webrunner. This feature is not officially supported.


ウェブインタフェースを介してテストを実行するための代替スクリプトがあります:<syntaxhighlight lang="php">admin/tool/phpunit/webrunner.php</syntaxhighlight> これはテストサーバ上でコマンドラインインターフェイスを使用できない場合の最後の手段として使用してください。コマンドラインおよびwebrunnerの両方から実行しようとした場合、恐らくパーミッションの問題で壊れてしまうでしょう。この機能は公式にはサポートされていません。


===How to run only some tests===
===一部のテストのみ実行する方法===
==== Running a single test quickly (PHPUnit 9) ====
==== 単一のテストをすばやく実行する (PHPUnit 9) ====
{{Moodle 3.11}}
{{Moodle 3.11}}
The fastest way to run a single test in PHPUnit 9.5 and higher (Moodle 3.11 and higher) is to use the filter argument:
PHPUnit 9.5以降 (Moodle 3.11以降) で単一のテストを実行するには filter引数を使用するのが一番簡単な方法です:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter tool_dataprivacy_metadata_registry_testcase
vendor/bin/phpunit --filter tool_dataprivacy_metadata_registry_testcase
</syntaxhighlight>
</syntaxhighlight>
To run all tests provided by the single component, use suite and the name it has in the phpunit.xml file. Example:
ひとつのコンポーネントが提供するすべてのテストを実行するにはphpunit.xmlファイルにあるsuiteおよび名称を使用してください。例:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit --testsuite workshopform_accumulative_testsuite
vendor/bin/phpunit --testsuite workshopform_accumulative_testsuite
388行目: 146行目:
vendor/bin/phpunit -c mod/workshop/form/accumulative/
vendor/bin/phpunit -c mod/workshop/form/accumulative/
</syntaxhighlight>
</syntaxhighlight>
==== Running a single test quickly (PHPUnit 8) ====
==== 単一のテストをすばやく実行する (PHPUnit 8) ====
{{Moodle 3.10}}
{{Moodle 3.10}}
The fastest way to run a single test in PHPUnit 8.5 and lower (Moodle 3.10 and lower):
PHPUnit 8.5以降 (Moodle 3.10以降) で単一のテストを実行するための最速の方法です:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit my/tests/filename.php
vendor/bin/phpunit my/tests/filename.php
</syntaxhighlight>
</syntaxhighlight>
so, run this command in the CLI to see a real test in action:
実際のテストを確認するにはこのコマンドをCLIで実行してください:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit cohort/tests/cohortlib_test.php
vendor/bin/phpunit cohort/tests/cohortlib_test.php
</syntaxhighlight>
</syntaxhighlight>
You can also run a single test method inside a class:
また、あなたはクラス内で1つのテストメソッドを実行できます:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter test_function_name path/to/file.php
vendor/bin/phpunit --filter test_function_name path/to/file.php
</syntaxhighlight>
</syntaxhighlight>
Note: You should be careful because it may be possible to run tests this way which are not included in the normal run if, for example, the file is not placed in the correct location. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.
注意: この方法でテストを実行した場合、例えばファイルが正しい場所に置かれていない時に通常の実行に含まれないテストを実行する可能性があるため、注意が必要です。あなたがこの方法を使用する場合、少なくとも1回は完全なテスト (または以下のように --group run)を実行してテストが検出されることを確認してください。


Filters can also be applied to capture a group of similar tests across all testsuites:
フィルタを適用して、すべてのテストスイートで類似したテストグループもキャプチャできます:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit --filter test_flag_user
vendor/bin/phpunit --filter test_flag_user
</syntaxhighlight>
</syntaxhighlight>
It is also possible to run all tests in a component (subsystem or plugin) by using the testsuite option:
testsuiteオプションを使用して、コンポーネント (サブシステムまたはプラグイン) 内のすべてテストの実行も可能です:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
vendor/bin/phpunit --testsuite mod_forum_testsuite
vendor/bin/phpunit --testsuite mod_forum_testsuite
414行目: 172行目:
vendor/bin/phpunit --testsuite core_privacy_testsuite --filter test_component_is_compliant
vendor/bin/phpunit --testsuite core_privacy_testsuite --filter test_component_is_compliant
</syntaxhighlight>
</syntaxhighlight>
==== Using the @group annotation ====
==== @groupアノテーションを使用する ====
If you add annotations like
あなたがプラグイン内すべてのクラスにアノテーションを以下のように追加している場合、
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
namespace qtype_stack;
namespace qtype_stack;
424行目: 182行目:
class cas_keyval_test extends \basic_testcase {
class cas_keyval_test extends \basic_testcase {
</syntaxhighlight>
</syntaxhighlight>
to all the classes in your plugin, then you can run just the tests for your plugin by doing
以下のようにプラグインのテストを実行できます:
  phpunit --group qtype_stack
  phpunit --group qtype_stack
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.
したがって、あなたのプラグインにすべてのテストでフランケンスタイル名の注釈をお勧めします。
==== Using multiple phpunit.xml files ====
==== 複数のphpunit.xmlファイルを使用する ====
It's easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with
どのテストを一緒に実行しなければならないか定義したphpunit.xmlファイルを代わりに作成するのは簡単です。参考までにテスト環境が初期化された後、あなたのベースディレクトリにあるデフォルトの/phpunit.xmlを確認してください。あなたはカスタムファイルを作成した後、次のコマンドでテストを実行できます:
  vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml
  vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml
Also, for commodity, you can use this command:
また、あなたは日常的な使用に関して次のコマンドを使用できます:
  php admin/tool/phpunit/cli/util.php --buildcomponentconfigs
  php admin/tool/phpunit/cli/util.php --buildcomponentconfigs
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like
これは各コンポーネント (プラグインまたはサブシステム) およびその他の重要なディレクトリに有効なphpunit.xmlファイルを自動的に作成します。そして、あなたは次のようなテストコマンドを実行できます:
  vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // Note that it's not needed to specify the name of the file (if it is 'phpunit.xml').
  vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // ファイル名が「phpunit.xml」の場合、ファイル名を指定する必要がないことに留意してください。
  vendor/bin/phpunit -c question
  vendor/bin/phpunit -c question
  vendor/bin/phpunit -c question/type/calculated
  vendor/bin/phpunit -c question/type/calculated
439行目: 197行目:
  vendor/bin/phpunit -c lib/dml
  vendor/bin/phpunit -c lib/dml
  ...
  ...
or, also
または
  cd directory/with/phpunit.xml
  cd directory/with/phpunit.xml
  phpunit
  phpunit
=External test resources=
=外部テストリソース=
{{Moodle 2.6}}
{{Moodle 2.6}}
By default Moodle phpunit tests contact http://download.moodle.org server when testing curl related functionality. Optionally you may checkout a local copy of the test scripts and access it instead:
デフォルトではMoodle phpunitでcurl関連機能をテストする場合、http://download.moodle.orgサーバにコンタクトします。オプションとして、あなたはテストスクリプトのローカルコピーをチェックアウトして代わりにそれにアクセスできます:
# clone https://github.com/moodlehq/moodle-exttests to web directory
# clone https://github.com/moodlehq/moodle-exttests to web directory
# add to your config.php or modify phpunit.xml file <syntaxhighlight lang="php">define('TEST_EXTERNAL_FILES_HTTP_URL', 'http://localhost/moodle-exttests');</syntaxhighlight>
# ウェブディレクトリに https://github.com/moodlehq/moodle-exttests をクローンしてください。
=Writing new tests=
# あなたのconfig.phpファイルに追加するか、phpunit.xmlファイルを修正してください:<syntaxhighlight lang="php">define('TEST_EXTERNAL_FILES_HTTP_URL', 'http://localhost/moodle-exttests');</syntaxhighlight>
* read [http://www.phpunit.de/manual/current/en/ official PHPUnit online documentation]
=新しいテストを書く=
* see [[Writing PHPUnit tests]]
* [http://www.phpunit.de/manual/current/en/ 公式PHPUnitオンラインドキュメンテーション]をご覧ください。
=Conversion of existing SimpleTests=
* [[PHPUnitテストを書く]]をご覧ください。
* see [[SimpleTest conversion]]
=既存のSimpleTestsの変換=
=PHPUnit support in IDEs=
* [[SimpleTestコンバージョン]]をご覧ください。
* [[Setting up Eclipse]]
=IDEにおけるPHPUnitのサポート=
* [[Setting up Netbeans]]
* [[Eclipseを設定する]]
* [[Setting up PhpStorm]]
* [[Netbeansを設定する]]
=Common Unit Test Problems=
* [[PhpStormを設定する]]
[[Common unit test problems]]
=単体テストでよくある問題=
=Performance=
[[単体テストでよくある問題]]
A typical run of full PHPUnit tests for Moodle 2.7 takes 10-20 minutes depending on the machine. If tests run slowly for you:
=パフォーマンス=
* Ensure you are using a database and filesystem running on the same machine that is running the tests (not on a remote server).
Moodle 2.7のPHPUnit テストをフルに実行した場合、マシンにもよりますが通常10-20分ほどで終了します。もしテストの実行が遅い場合:
* Apply developer-only performance settings to your database: [[Postgres Tuning For Developers]]
* あなたがテストを実行しているのと同じマシン上でデータベースおよびファイルシステムが動作していることを確認してください (リモートサーバではない)
=Command line tips=
* 開発者専用のパフォーマンス設定をデータベースに適用してください: [[開発者のためのPostgresチューニング]]
* Run all tests for a plugin (mod_mymodule in this example): vendor/bin/phpunit --testsuite=mod_mymodule_testsuite  
=コマンドラインヒント Command line tips=
* Run only named test: vendor/bin/phpunit --filter=test_my_test_function_name
* プラグインのすべてのテストを実行してください (この例では mod_mymodule) : vendor/bin/phpunit --testsuite=mod_mymodule_testsuite  
* Display each test name before running it (useful e.g. if it crashes before the end and you want to know which test it was running at that point) vendor/bin/phpunit --debug (you will probably want to redirect this to a file as it gets very long).
* 名前付きテストのみ実行してください: vendor/bin/phpunit --filter=test_my_test_function_name
[[Category:Unit testing]]
* 実行前に各テストの名前を表示してください (終了前にテストがクラッシュして、その時点でどのテストを実行していたかを知りたい場合に有用です) vendor/bin/phpunit --debug (非常に長くなるため、恐らくこれをファイルにリダイレクトしたいでしょう)
[[Category:単体テスト]]


[[en:PHPUnit]]
[[en:dev/PHPUnit]]

2022年4月16日 (土) 15:04時点における最新版

PHPUnitとは

Sebastian BergmannによるPHPUnit はPHP用の高度な単体テストフレームワークです。このフレームワークはComposerの依存ファイルとしてインストールされ、Moodleのインストールには含まれません。PHPUnitテストを実行するにはあなたの開発用コンピュータまたはテストサーバにPHPUnitを手動でインストールする必要があります。

以下の素晴らしいガイドをお読みください:

composer経由のPHPUnitインストール

  • Composerをインストールする

すべてのプラットフォームへのcomposerのインストールに関するインストラクションは次をご覧ください: https://getcomposer.org/download/

あなたのMoodleフォルダにcomposerr.pharファイルをインストールしてください。

  • Composerインストーラを実行してください。
cd /your/moodle/dirroot
php composer.phar install

(接続エラーが発生した場合、以下のコマンドをお試しください ... )

php composer.phar install --prefer-source

トラブルシューティング:

  • あなたがWindowsでプロクシを使用している場合、HTTP_PROXY環境変数にHTTPプロクシのアドレスおよびポートを設定することでComposerがファイルを正しくダウンロードできるようになります。
  • あなたはcomposeインストール時にgithubの認証情報の入力を求められる場合があります。
    • これはgithubによるレート制限を回避するための個人用アクセストークンの生成に使用されます。
    • あなたがgithubアカウントで2要素認証を有効にしている場合、または自分で認証情報を提供したくない場合、手動でトークンを生成する必要があります:
    • ( このバグに関する詳細は composer issue #2280 をご覧ください)

詳細インストラクション:

詳細インストラクション:

前のPEARベースバージョンをアンインストールする

composerを使用する前、このページではPEAR経由でのphpunitインストールを推奨していました。あなたがもしそうしていたのでしたら、今すぐそのパッケージをアンインストールした方がよいでしょう。以下のコマンドでアンインストールできるはずです:

  $ pear uninstall phpunit/DbUnit
  $ pear uninstall phpunit/PHPUnit

PHPUnitバージョン

以下の表はデフォルトのコンポーザ設定を使用した場合、どのMoodleバージョンでどのPHPUnitがインストールされるか示しています。

Moodleバージョン PHPUnitバージョン リンク
Moodle 3.11 PHPUnit 9.5 ドキュメンテーション
Moodle 3.10 PHPUnit 8.5 ドキュメンテーション
Moodle 3.7 - 3.9 PHPUnit 7.5 ドキュメンテーション
Moodle 3.4 - 3.6 PHPUnit 6.5 ドキュメンテーション
Moodle 3.2 - 3.3 PHPUnit 5.5 ドキュメンテーション
Moodle 3.1 PHPUnit 4.8.27 ドキュメンテーション

テスト環境のインストレーション

PHPUnitの統合には専用のデータベースおよびdatarootが必要です。まず最初にconfig.phpに新しい datarootディレクトリおよび接頭辞を追加してください。config-dist.phpに例があります (「セクション 9」までスクロールダウンしてください)。

$CFG->phpunit_prefix = 'phpu_';
$CFG->phpunit_dataroot = '/home/example/phpu_moodledata';

PHPUnitのテストの中にはインターネットへのライブ接続を必要とするものがあります。システムがインターネットに直接接続されていない場合、config.phpでプロキシを指定する必要があります - 通常は管理設定ユーザインターフェイスで指定します (あなたがプロクシを使用していない場合、このステップは省略してください)。正しい値がわからない場合、関連する管理者設定ページまたはデータベースのmdl_configテーブルで設定を確認してください。

//  通常のプロキシ設定
$CFG->proxyhost = 'wwwcache.example.org';
$CFG->proxyport = 80;
$CFG->proxytype = 'HTTP';
$CFG->proxybypass = 'localhost, 127.0.0.1, .example.org';
// プロキシがユーザ名/パスワードを必要としない場合は次の行を省略してください:
$CFG->proxyuser = 'systemusername';
$CFG->proxypassword = 'systempassword';

Moodle 2.8.5以降、単体テストのために特定のデータベース設定を提供することもできます (提供されない場合、標準のデータベース設定が使用されます):

$CFG->phpunit_dbtype    = 'pgsql';      // 'pgsql', 'mariadb', 'mysqli', 'mssql', 'sqlsrv' または 'oci'
$CFG->phpunit_dblibrary = 'native';     // 現在、'native' のみ
$CFG->phpunit_dbhost    = '127.0.0.1';  // 例 'localhost' または 'db.isp.com' または IP
$CFG->phpunit_dbname    = 'mytestdb';     // データベース名 例) moodle
$CFG->phpunit_dbuser    = 'postgres';   // あなたのデータベースユーザ名
$CFG->phpunit_dbpass    = 'some_password';   // あなたのデータベースパスワード

そして、あなたは以下のコマンドでテスト環境を初期化する必要があります。

cd /home/example/moodle
php admin/tool/phpunit/cli/init.php

このコマンドはアップグレード、プラグインのインストールおよびアンインストール、開発中のプラグインへのテスト追加等の場合に繰り返し実行する必要があります。

注意: 個々のinitスクリプトが繰り返し呼び出すため、php cliの実行ファイル (またはあなたが使用したいもの) が正しくパス上にあることを確認してください。また、en_AUロケールがサーバにインストールされていることを確認してください。

LDAP

あなたがLDAP単体テストを実行したい場合、テストサーバ上で動作する設定済みのLDAP環境を準備する必要があります。基本的なLDAPツリー構造がない場合、「Search: No such object」 とメッセージが表示されてテストが失敗します。新しいシェルプロンプトでLDAPツリー構造を構築してください:

$ ldapadd -H ldap://127.0.0.1 -D "cn=admin,dc=yourcomputer,dc=local" -W
dn:dc=yourcomputer,dc=local
objectClass:dcObject
objectClass:organizationalUnit
dc:yourcomputer
ou:YOURCOMPUTER

あなたのテスト用LDAP環境を探す場所をconfig.phpでMoodleに教えてください:

// auth/ldapテスト用の定数
define('TEST_AUTH_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_AUTH_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_AUTH_LDAP_BIND_PW', '*');
define('TEST_AUTH_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');

// enrol/ldapテスト用定数
define('TEST_ENROL_LDAP_HOST_URL', 'ldap://127.0.0.1');
define('TEST_ENROL_LDAP_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_ENROL_LDAP_BIND_PW', '*');
define('TEST_ENROL_LDAP_DOMAIN', 'dc=yourcomputer,dc=local');

// lib/ldapのテスト用定数
define('TEST_LDAPLIB_HOST_URL', 'ldap://127.0.0.1');
define('TEST_LDAPLIB_BIND_DN', 'cn=admin,dc=yourcomputer,dc=local');
define('TEST_LDAPLIB_BIND_PW', '*');
define('TEST_LDAPLIB_DOMAIN', 'dc=yourcomputer,dc=local');

テスト実行

すべてのテストスイートをメイン設定ファイルから実行したい場合、あなたの

vendor/bin/phpunit

スクリプト

$CFG->dirroot

をディレクトリで実行してください。

cd /home/example/moodle
vendor/bin/phpunit

残りの例では

phpunit

を使用していますが、

vendor/bin/phpunit

と置換するか、あなたはディレクトリにショートカットを作成する必要があります。 あなたはIDE でPHPUnit の設定ファイルへのパスを指定する必要がある場合があります。あなたの

$CFG->dirroot

から

phpunit.xml

への絶対パスを使用してください。 ウェブインタフェースを介してテストを実行するための代替スクリプトがあります:

admin/tool/phpunit/webrunner.php

これはテストサーバ上でコマンドラインインターフェイスを使用できない場合の最後の手段として使用してください。コマンドラインおよびwebrunnerの両方から実行しようとした場合、恐らくパーミッションの問題で壊れてしまうでしょう。この機能は公式にはサポートされていません。

一部のテストのみ実行する方法

単一のテストをすばやく実行する (PHPUnit 9)

テンプレート:Moodle 3.11 PHPUnit 9.5以降 (Moodle 3.11以降) で単一のテストを実行するには filter引数を使用するのが一番簡単な方法です:

vendor/bin/phpunit --filter tool_dataprivacy_metadata_registry_testcase

ひとつのコンポーネントが提供するすべてのテストを実行するにはphpunit.xmlファイルにあるsuiteおよび名称を使用してください。例:

vendor/bin/phpunit --testsuite workshopform_accumulative_testsuite

Alternatively if you have config files built for each component:

vendor/bin/phpunit -c mod/workshop/form/accumulative/

単一のテストをすばやく実行する (PHPUnit 8)

テンプレート:Moodle 3.10 PHPUnit 8.5以降 (Moodle 3.10以降) で単一のテストを実行するための最速の方法です:

vendor/bin/phpunit my/tests/filename.php

実際のテストを確認するにはこのコマンドをCLIで実行してください:

vendor/bin/phpunit cohort/tests/cohortlib_test.php

また、あなたはクラス内で1つのテストメソッドを実行できます:

vendor/bin/phpunit --filter test_function_name path/to/file.php

注意: この方法でテストを実行した場合、例えばファイルが正しい場所に置かれていない時に通常の実行に含まれないテストを実行する可能性があるため、注意が必要です。あなたがこの方法を使用する場合、少なくとも1回は完全なテスト (または以下のように --group run)を実行してテストが検出されることを確認してください。

フィルタを適用して、すべてのテストスイートで類似したテストグループもキャプチャできます:

vendor/bin/phpunit --filter test_flag_user

testsuiteオプションを使用して、コンポーネント (サブシステムまたはプラグイン) 内のすべてテストの実行も可能です:

vendor/bin/phpunit --testsuite mod_forum_testsuite
vendor/bin/phpunit --testsuite core_privacy_testsuite
vendor/bin/phpunit --testsuite core_privacy_testsuite --filter test_component_is_compliant

@groupアノテーションを使用する

あなたがプラグイン内すべてのクラスにアノテーションを以下のように追加している場合、

namespace qtype_stack;
/**
 * Unit tests for {@link stack_cas_keyval} @ qtype/stack/tests/cas_keyval_test.php.
 * @group qtype_stack
 */
class cas_keyval_test extends \basic_testcase {

以下のようにプラグインのテストを実行できます:

phpunit --group qtype_stack

したがって、あなたのプラグインにすべてのテストでフランケンスタイル名の注釈をお勧めします。

複数のphpunit.xmlファイルを使用する

どのテストを一緒に実行しなければならないか定義したphpunit.xmlファイルを代わりに作成するのは簡単です。参考までにテスト環境が初期化された後、あなたのベースディレクトリにあるデフォルトの/phpunit.xmlを確認してください。あなたはカスタムファイルを作成した後、次のコマンドでテストを実行できます:

vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml

また、あなたは日常的な使用に関して次のコマンドを使用できます:

php admin/tool/phpunit/cli/util.php --buildcomponentconfigs

これは各コンポーネント (プラグインまたはサブシステム) およびその他の重要なディレクトリに有効なphpunit.xmlファイルを自動的に作成します。そして、あなたは次のようなテストコマンドを実行できます:

vendor/bin/phpunit -c mod/forum[/phpunit.xml]  // ファイル名が「phpunit.xml」の場合、ファイル名を指定する必要がないことに留意してください。
vendor/bin/phpunit -c question
vendor/bin/phpunit -c question/type/calculated
vendor/bin/phpunit -c backup
vendor/bin/phpunit -c lib/dml
...

または

cd directory/with/phpunit.xml
phpunit

外部テストリソース

Moodle 2.6

デフォルトではMoodle phpunitでcurl関連機能をテストする場合、http://download.moodle.orgサーバにコンタクトします。オプションとして、あなたはテストスクリプトのローカルコピーをチェックアウトして代わりにそれにアクセスできます:

  1. clone https://github.com/moodlehq/moodle-exttests to web directory
  2. ウェブディレクトリに https://github.com/moodlehq/moodle-exttests をクローンしてください。
  3. あなたのconfig.phpファイルに追加するか、phpunit.xmlファイルを修正してください:
    define('TEST_EXTERNAL_FILES_HTTP_URL', 'http://localhost/moodle-exttests');
    

新しいテストを書く

既存のSimpleTestsの変換

IDEにおけるPHPUnitのサポート

単体テストでよくある問題

単体テストでよくある問題

パフォーマンス

Moodle 2.7のPHPUnit テストをフルに実行した場合、マシンにもよりますが通常10-20分ほどで終了します。もしテストの実行が遅い場合:

  • あなたがテストを実行しているのと同じマシン上でデータベースおよびファイルシステムが動作していることを確認してください (リモートサーバではない)。
  • 開発者専用のパフォーマンス設定をデータベースに適用してください: 開発者のためのPostgresチューニング

コマンドラインヒント Command line tips

  • プラグインのすべてのテストを実行してください (この例では mod_mymodule) : vendor/bin/phpunit --testsuite=mod_mymodule_testsuite
  • 名前付きテストのみ実行してください: vendor/bin/phpunit --filter=test_my_test_function_name
  • 実行前に各テストの名前を表示してください (終了前にテストがクラッシュして、その時点でどのテストを実行していたかを知りたい場合に有用です) vendor/bin/phpunit --debug (非常に長くなるため、恐らくこれをファイルにリダイレクトしたいでしょう)。