robotframework 使い方 2(モジュールの作り方)
この記事は、先日の記事の続きです。
今回は、robotframework のライブラリ作成方法の備忘録です。
java は好きじゃないので、pythonとrobotframework のみを書きます。
ライブラリの作成方法
- java or python or Robotframeworkのスクリプトで作成できる。
ファイルの構成
とりあえず、下記の構成で作成した。
./
├── modules
│ ├── module.py (テストソース用pythonモジュール)
│ ├── module.robot (テストソース用ライブラリ)
│ ├── resource.py (テスト用リソースファイル)
│ └── test.json
├── sample2.robot (テストソース)
├── sample2 (実行結果出力先ディレクトリ)
├── log.html
├── output.xml
└── report.html
作成例
python自作モジュール
from datetime import datetime
var_py2 = "this is python var"
def get_UnixtimeNow():
# 他のモジュールの関数が使えることを確認する
now = datetime.now()
return now.timestamp()
def add_dict_timestamp(dic: dict):
# dicの操作ができることを確認する
dic["timestamp"] = datetime.now().timestamp()
return dic
def add_comment(text: str):
# 作成したモジュールでstringの操作ができることを確認する
return "[{}] {}".format(add_comment.__name__, text)
def calc(i: int):
# 作成したモジュールで計算ができることを確認する
return i + 10
robotframework自作ライブラリ
* Settings *
Library Process
Library String
Library OperatingSystem
* Variables *
${address} aaaaa@gmail.com
* Keywords *
ファイル作成
[Arguments] ${dirindex}
[Documentation] 指定したディレクトリを作成し、0~9の空ファイルを作成する。
Run Process mkdir -p ${dirindex}
FOR ${index} IN RANGE 0 10
Run Process touch ${dirindex}/${index}.txt
File Should Exist ${dirindex}/${index}.txt
END
テストコード
* Settings *
Documentation このソースコードはRobotframeworkのモジュールを作成して試すものです。
Resource ./modules/module.robot # Robotframeworkのライブラリ
# pythonのモジュールはimportできるように設定してあること
Library module # pythonのモジュール
Variables ./modules/resource.py
* Variables *
# 普通の変数作成
${NAME} Robot Framework
${MULTILINE} SEPARATOR=\n First line
... Second line Third line
# list作成方法
@{NAMES} Matti Teppo
@{NAMES2} @{NAMES} Seppo
@{NOTHING}
@{MANY} one two three four
... five six seven
# dict作成方法
&{dic} name=Mat age=20 sex=\?
* Test cases *
case0
[Documentation] ログレベルセット
Log Variables INFO
Set Log Level TRACE
Set Test Message Test message
case1
[Documentation] このテストではmodule.robotで作成したkeywordsが
... このソースでも使えることを確認する。${\n}
[Tags] module`s function
Set Test Message module`s function test1
ファイル作成 ./worksample/test1
ファイル作成 ./worksample/test2
case2
[Documentation] このテスtではmodule.pyで作成した関数が
... このソースでも使えることを確認する(pythonの関数名はsnake caseで作成)
[Tags] module`s function
Set Test Message module`s function test2
${out} = Get UnixtimeNow
${out2} = Add Dict Timestamp ${dic}
${out3} = Add Comment モジュール関数
${out4} = calc 10
Log ${out}
Log ${out2}
Log ${out3}
Log cale: 10+10 = ${out4}
case3
[Documentation] このテストではmodule.robotで設定したファイルの変数が
... ここでも使えることを確認する。
[Tags] module`s variable
Set Test Message module`s function test3
# resoreceでインポートした変数は使える。
Variable Should Exist ${address}
Log [modlue variables]${address}
case4
[Documentation] このテストではmodule.py,resorce.pyで設定したファイルの変数が
... ここでも使えるor使えないことを確認する。
[Tags] module`s variable
Set Test Message module`s function test4
#
Variable Should Exist ${var_py}
Variable Should Exist ${timestamp}
Variable Should Exist ${json_data}
Log [resource variables]${var_py}
Log [resource variables]${timestamp}
Log [resource variables]${json_data}
# pythonでモジュールとしてインポートした変数は使えない
Variable Should Not Exist ${var_py2}
実行結果
user@MacBook Robots % robot --outputdir sample2 sample2.robot
==============================================================================
Sample2 :: このソースコードはRobotframeworkのモジュールを作成して試すもので...
==============================================================================
case0 :: ログレベルセット | PASS |
Test message
------------------------------------------------------------------------------
case1 :: このテストではmodule.robotで作成したkeywordsが このソース... | PASS |
module`s function test1
------------------------------------------------------------------------------
case2 :: このテスtではmodule.pyで作成した関数が このソースでも使え... | PASS |
module`s function test2
------------------------------------------------------------------------------
case3 :: このテストではmodule.robotで設定したファイルの変数が ここ... | PASS |
module`s function test3
------------------------------------------------------------------------------
case4 :: このテストではmodule.py,resorce.pyで設定したファイルの変... | PASS |
module`s function test4
------------------------------------------------------------------------------
Sample2 :: このソースコードはRobotframeworkのモジュールを作成して... | PASS |
5 critical tests, 5 passed, 0 failed
5 tests total, 5 passed, 0 failed
==============================================================================
Output: ./sample2/output.xml
Log: ./sample2/log.html
Report: ./sample2/report.html
まとめ
- pythonで作成したライブラリ
- 作成した関数は関数名の"_"が" "に変換されたkeywordとして使うことができる (モジュールで定義した定数は使えない?)
- resourceとして読み込むことで変数を読み込んで使うことができる。
- robotframeworkで作成したライブラリ
- keywordも変数も読み込んで使うことができる。
以上、要望があれば何か書きます。