Robot Frameworkでは、キーワードの仮引数を使う方法として
- 埋め込み引数
[Arguments]
設定のある引数
の2つがあります。
以前、このあたりで試しました。
ただ、両方を使おうとするとエラーとなるため、注意が必要です。
試しに、
*** Keywords *** 埋め込みキーワード「${embed}」のみ log to console ${embed} Argumentsのみ [Arguments] ${arg} log to console ${arg} キーワード「${embed}」とArgumentsの両方 [Arguments] ${arg} log to console ${embed} log to console ${arg} *** Test Cases *** 埋め込み引数のみのキーワードを使う 埋め込みキーワード「埋め込み」のみ Arguments設定がある引数のみのキーワードを使う Argumentsのみ arg=Argument設定 両方の引数を使う キーワード「埋め込み」とArgumentsの両方 arg=Argument設定
というテストケースを用意します。
実行してみます。
$ robot test_keyword_args.robot [ ERROR ] Error in test case file '/path/to/builtin_library_samples/test_keyword_args.robot': Creating keyword 'キーワード「${embed}」とArgumentsの両方' failed: Keyword cannot have both normal and embedded arguments. ======================== Test Keyword Args ======================== 埋め込み引数のみのキーワードを使う 埋め込み 埋め込み引数のみのキーワードを使う | PASS | --------------------------------- Arguments設定がある引数のみのキーワードを使う Argument設定 Arguments設定がある引数のみのキーワードを使う | PASS | --------------------------------- 両方の引数を使う | FAIL | No keyword with name 'キーワード「埋め込み」とArgumentsの両方' found. --------------------------------- Test Keyword Args | FAIL | 3 critical tests, 2 passed, 1 failed 3 tests total, 2 passed, 1 failed
と、両方使った場合のみ、エラーが発生しました。
エラーメッセージにも
Keyword cannot have both normal and embedded arguments.
とあり、どちらかしか使えないことがわかります。
両者の差は
埋め込み引数には、通常のキーワードの引数で使えるような、デフォルト値や可変個の引数のサポートがありません。 キーワードを呼ぶときに、変数を指定するのはもちろん可能ですが、テストの可読性を下げてしまうかもしれません。 埋め込み引数を使えるのは、ユーザキーワードだけです。
基本の書き方 - キーワード名の中に引数を埋め込む - ユーザキーワードを定義する — RobotFramework和訳・日本語ドキュメント集
です。
そのため、両者は
- 引数のデフォルト値や可変個の引数が必要か
- キーワードの可読性をどうするか
を考慮して使い分ければ良さそうです。
ソースコード
GitHubに上げました。 builtin_library_samples/test_keyword_args.robot
ファイルが、今回作成したファイルです。
thinkAmi-sandbox/RobotFramework-sample: Robot Framewrok samples