「PHPUnit」の版間の差分

提供:MoodleDocs
移動先:案内検索
編集の要約なし
編集の要約なし
169行目: 169行目:
注意: この方法でテストを実行した場合、例えばファイルが正しい場所に置かれていない時に通常の実行に含まれないテストを実行する可能性があるため、注意が必要です。あなたがこの方法を使用する場合、少なくとも1回は完全なテスト (または以下のように --group run)を実行して、テストが検出されることを確認してください。
注意: この方法でテストを実行した場合、例えばファイルが正しい場所に置かれていない時に通常の実行に含まれないテストを実行する可能性があるため、注意が必要です。あなたがこの方法を使用する場合、少なくとも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:
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
180行目: 180行目:
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アノテーションを使用する Using the @group annotation ====
If you add annotations like
If you add annotations like
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">

2022年3月21日 (月) 15:02時点における版

作成中です - Mitsuhiro Yoshida (トーク)

PHPUnitとは

Sebastian BergmannによるPHPUnit はPHP用の高度な単体テストフレームワークです。このフレームワークはComposerの依存ファイルとしてインストールされ、Moodleのインストールには含まれません。PHPUnitテストを実行するにはあなたの開発用コンピュータまたはテストサーバに手動で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要素認証を有効にしている場合、または自分で認証情報を提供したくない場合、手動でトークンを生成する必要があります:
    • ( このバグに関する詳細は 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の両方から実行しようとした場合、恐らくパーミッションの問題で壊れてしまうでしょう。この機能は公式にはサポートされていません。

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

1つのテストをすばやく実行する (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/

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

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

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

so, run this command in the CLI to see a real test in action: 実際のテストを確認するにはこのコマンドをCLIで実行してください:

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

You can also run a single test method inside a class: また、あなたはクラス内で1つのテストメソッドを実行できます:

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

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

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

vendor/bin/phpunit --filter test_flag_user

It is also possible to run all tests in a component (subsystem or plugin) by using the testsuite option: 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アノテーションを使用する Using the @group annotation

If you add annotations like

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 {

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:

  1. clone https://github.com/moodlehq/moodle-exttests to web directory
  2. add to your config.php or modify phpunit.xml file
    define('TEST_EXTERNAL_FILES_HTTP_URL', 'http://localhost/moodle-exttests');
    

Writing new tests

Conversion of existing SimpleTests

PHPUnit support in IDEs

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).