LoginSignup
2

More than 5 years have passed since last update.

RobotFrameworkに入門したときに躓いたポイント

Posted at

はじめに

ネットワーク機器のテストなどによく使われている(?)RobotFrameworkに触れる機会があったので、その際に躓いたポイントを列挙します。Pythonなどのプログラミング言語と異なる点と捉えていただいても良いかもしれません。

またこの記事を書いておいてなんですが、RobotFrameworkは日本語ドキュメントが充実しているので、ちゃんと読んでいればここに記述してある点で引っかかることも少ないかと思います (読み落として躓いてしまったのが私です)

キーワードの引数の記述には空白が2個以上必要

空白が2個以上もしくはタブ文字がない場合、キーワードの一部として認識されます。

# エラー
Should Be True ${condition}
# 正常
Should Be True  ${condition}

文字列リテラル以外の記述方法

RobotFrameworkでは装飾なしで記述されたものは文字列として扱われます。
変数記法${}を使用すると、応じた型に変換してくれます。

# 文字列
${string} =  Set Variable  2

# 数値
${number1} =  Set Variable  ${2}
${number2} =  Set Variable  ${3.14}
${number3} =  Set Variable  ${-1e-4}

# 真偽
${t} =  Set Variable  ${true}
${f} =  Set Variable  ${false}

# None
${n} =  Set Variable  ${None}

Pythonのインスタンス作成

キーワード中でPythonのインスタンス生成を行いたい場合、ライブラリとして読み込む必要があります。
ライブラリとして読み込んだ場合、インスタンス生成まで行われるようです。
よって複数のインスタンスが必要となる場合、読み込んだライブラリに別名を付けましょう。

# 例クラス
class ExampleClass():
  def __init__(self, arg1):
    print('init')

  def function(self):
    print('function')

  def func_tion2(self):
    print('func_tion2')
# Settingsに記述
*** Settings ***
Library  example_module.ExampleClass  arg1
Library  example_module.ExampleClass  arg1  WITH NAME  ExampleClass2

# テストケース中に記述
Import Library  example_module.ExampleClass  arg1
Import Library  example_module.ExampleClass  arg1  WITH NAME  ExampleClass2

# 使用
ExampleClass2.function
ExampleClass2.func tion2

注意点

ライブラリとして読み込むため通常の変数とスコープが異なります。
ちゃんと調べられておらず申し訳ないですが、少なくともテストケーススコープ以上の範囲になるようです。
Import Libraryキーワードを使用して通常のPythonのインスタンスと同様に扱って痛い目を見ました。

参考

Robot Framework 和訳・日本語ドキュメント集
StackOverflow - Creating instances of python class from Robotframework

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
2