この記事は「Robot Framework Advent Calendar 2017 - Qiita」の8日目の記事です。
Robot Frameworkのデフォルト設定では、テストの実行後にログファイルやレポートファイルが自動生成されます。
ログファイル
もしテストが失敗した場合、その詳細情報がログファイルに出力されます。
レポートファイル(すべてのテストがパスした時)
レポートファイル(いずれかのテストが失敗した時)
また、自動生成されるファイル名のデフォルト名称は
です。
デフォルト設定では、これらのファイルはテストを実行した時のカレントディレクトリに出力されます。
また、自動生成されるファイル以外にも、
- xUnitと互換性のあるファイル
- デバッグファイル
も追加で出力できます。
そこで今回は、それらのファイル出力を試してみます。
目次
- 環境
- テストコード中でファイル名を変更 (不可)
- コマンドラインオプションを使う
- 出力ファイルがいらない場合
- xUnitのログと互換性のあるファイルが欲しい場合
- デバッグファイルが欲しい場合
- ファイルにタイムスタンプが欲しい場合
環境
- Python 3.6.3
- Robot Framework 3.0.2
テストコード中でファイル名を変更 (不可)
レポートファイル名は、自動変数 ${REPORT FILE}
に設定されています。
- http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#automatic-variables
- http://robotframework-ja.readthedocs.io/ja/latest/userguide/CreatingTestData/Variables.html#id31
そこで、試しに ${REPORT FILE}
を変更してみます。
*** Variables *** # Variablesにてセット ${REPORT FILE} rename_report_in_variables.html *** TestCases *** レポートファイル名を変更する # 見やすいように改行する Log To Console ${EMPTY} # 変数としてセット Set Variable ${REPORT FILE} rename_report_in_test_cases1.html Log To Console ${REPORT FILE} # グローバルな変数にセット Set Global Variable ${REPORT FILE} rename_report_in_test_cases2.html Log To Console ${REPORT FILE}
実行結果です。
$ robot rename.robot ========================== Rename ========================== レポートファイル名を変更する ../path/to/report.html ..rename_report_in_test_cases2.html レポートファイル名を変更する | PASS | ------------------------------------ Rename | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ========================== Output: /path/to/output.xml Log: /path/to/log.html Report: /path/to/report.html
キーワード Set Global Variable
では値の書き換えはできたものの、最終的なログファイル名は変わりませんでした。
コマンドラインオプションを使う
ファイル名はコマンドラインオプションから変更できます。
ヘルプに記載がありました。
$ robot --help ... -o --output file XML output file. Given path, similarly as paths given to --log, --report, --xunit, and --debugfile, is relative to --outputdir unless given as an absolute path. Other output files are created based on XML output files after the test execution and XML outputs can also be further processed with Rebot tool. Can be disabled by giving a special value `NONE`. In this case, also log and report are automatically disabled. Default: output.xml -l --log file HTML log file. Can be disabled by giving a special value `NONE`. Default: log.html Examples: `--log mylog.html`, `-l NONE` -r --report file HTML report file. Can be disabled with `NONE` similarly as --log. Default: report.html
試してみます。
$ robot --log re_log.html --report re_report.html --output re_output.xml rename.robot =============== Rename =============== ... Output: /path/to/re_output.xml Log: /path/to/re_log.html Report: /path/to/re_report.html
各ファイル名が変更されました。
ちなみに、Robot Frameworkのコマンドラインオプションは、いずれもテストファイルよりも前に付ける必要があります。
上記例の場合、ファイル rename.robot
よりも前に書きます。書かない場合、
[ ERROR ] Parsing '--output' failed: Data source does not exist.
というエラーが出ます。
出力ファイルがいらない場合
なお、出力ファイルがいらない場合は、それぞれのコマンドラインオプションに None
を設定します。
$ robot --log None --report None --output None rename.robot ... Output: None
xUnitのログと互換性のあるファイルが欲しい場合
コマンドラインオプション --xunit (-x)
を使います。
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#xunit-compatible-result-file
$ robot --log None --report None --output None --xunit xunit_report.xml rename.robot ... Output: None XUnit: /path/to/xunit_report.xml
結果です。
<?xml version="1.0" encoding="UTF-8"?> <testsuite name="Rename" tests="1" errors="0" failures="1" skip="0"> <testcase classname="Rename" name="レポートファイル名を変更する" time="0"> <failure message="AssertionError" type="AssertionError"></failure> </testcase> </testsuite>
デバッグファイルが欲しい場合
コマンドラインオプション --debugfile (-b)
を使います。
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#debug-file
$ robot --log None --report None --output None --debugfile debug.txt rename.robot ... Debug: /path/to/debug.txt Output: None
デバッグ内容がプレーンテキストとして作成されます。
中身はこんな感じです。
============================================================================== 20171207 23:14:38.725 - INFO - + START SUITE: Rename [ ] ============================================================================== 20171207 23:14:38.725 - INFO - +- START TEST: レポートファイル名を変更する [ ] ------------------------------------------------------------------------------ 20171207 23:14:38.727 - INFO - +-- START KW: BuiltIn.Log To Console [ ${EMPTY} ] 20171207 23:14:38.727 - INFO - +-- END KW: BuiltIn.Log To Console (0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20171207 23:14:38.728 - INFO - +-- START KW: BuiltIn.Set Variable [ ${REPORT FILE} | rename_report_in_test_cases1.html ] 20171207 23:14:38.728 - INFO - +-- END KW: BuiltIn.Set Variable (1) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20171207 23:14:38.728 - INFO - +-- START KW: BuiltIn.Log To Console [ ${REPORT FILE} ] 20171207 23:14:38.728 - INFO - +-- END KW: BuiltIn.Log To Console (0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20171207 23:14:38.729 - INFO - +-- START KW: BuiltIn.Set Global Variable [ ${REPORT FILE} | rename_report_in_test_cases2.html ] 20171207 23:14:38.729 - INFO - ${REPORT FILE} = rename_report_in_test_cases2.html 20171207 23:14:38.729 - INFO - +-- END KW: BuiltIn.Set Global Variable (1) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20171207 23:14:38.729 - INFO - +-- START KW: BuiltIn.Log To Console [ ${REPORT FILE} ] 20171207 23:14:38.729 - INFO - +-- END KW: BuiltIn.Log To Console (0) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20171207 23:14:38.730 - INFO - +-- START KW: BuiltIn.Fail [ ] 20171207 23:14:38.730 - FAIL - AssertionError 20171207 23:14:38.730 - DEBUG - Traceback (most recent call last): None 20171207 23:14:38.730 - INFO - +-- END KW: BuiltIn.Fail (0) ------------------------------------------------------------------------------ 20171207 23:14:38.730 - INFO - +- END TEST: レポートファイル名を変更する (5) ------------------------------------------------------------------------------ 20171207 23:14:38.731 - INFO - + END SUITE: Rename (43) ==============================================================================
ファイルにタイムスタンプが欲しい場合
コマンドラインオプション --timestampoutputs (-T)
を使います。
$ robot --debugfile debug.txt --xunit xunit.xml --timestampoutputs rename.robot ... Debug: /path/to/debug-20171207-231818.txt Output: /path/to/output-20171207-231818.xml XUnit: /path/to/xunit-20171207-231818.xml Log: /path/to/log-20171207-231818.html Report: /path/to/report-20171207-231818.html
出力ファイルに関しては、他にも公式ドキュメントに書いてありますので、一度目を通しておくと良いかと思います。
3.5.1 Different output files - Robot Framework User Guide