LoginSignup
0
0

More than 1 year has passed since last update.

テスト自動化ツール  使い方メモ2(robotframework ライブラリ作成方法)

Last updated at Posted at 2020-12-12

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自作モジュール
module.py
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自作ライブラリ
module.robot
* 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

テストコード

sample2.robot
* 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

実行したログはこんな感じ
Screenshot_2020-12-13 Sample2 Log.png


まとめ

  1. pythonで作成したライブラリ
    1. 作成した関数は関数名の"_"が" "に変換されたkeywordとして使うことができる (モジュールで定義した定数は使えない?)
    2. resourceとして読み込むことで変数を読み込んで使うことができる。
  2. robotframeworkで作成したライブラリ
    1. keywordも変数も読み込んで使うことができる。

以上、要望があれば何か書きます。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0