2

Python のビヘイビアライブラリでは、次のようにパラメーター化されたシナリオ アウトラインを使用して機能ファイルを作成できます (このチュートリアルから適応)。

# feature file
Feature: Scenario Outline Example

  Scenario Outline: Use Blender with <thing>
    Given I put "<thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>"

    Examples: Amphibians
        | thing         | other thing |
        | Red Tree Frog | mush        |
        | apples        | apple juice |

対応するステップ定義は次のようになります。

# steps file
from behave   import given, when, then
from hamcrest import assert_that, equal_to
from blender  import Blender

@given('I put "{thing}" in a blender')
def step_given_put_thing_into_blender(context, thing):
    context.blender = Blender()
    context.blender.add(thing)

@when('I switch the blender on')
def step_when_switch_blender_on(context):
    context.blender.switch_on()

@then('it should transform into "{other_thing}"')
def step_then_should_transform_into(context, other_thing):
    assert_that(context.blender.result, equal_to(other_thing))

ご覧のとおり、機能ファイルからステップ関数にパラメーターを渡す方法は次のとおりです。

  • 山かっこで囲まれた機能ファイルでそれらを明示的に言及することにより
  • 次に、ステップ関数のデコレーターに、中括弧で囲まれた同じ単語を含めます (山括弧を再度使用しないのはなぜですか?!)。
  • 最後に、これらの単語をステップ関数の関数引数として挿入します。

ただし、多くの列を持つ大きなテーブルの例を考えると、これはすぐに読み書きが面倒になります。

# feature file
Feature: Large Table Example

  Scenario Outline: Use Blender with a lot of things
    Given I put "<first_thing>", "<second_thing>", "<third_thing>", "<fourth_thing>", "<fifth_thing>", "<sixth_thing>", "<seventh_thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>"

    Examples: Things
        | first thing | second thing | third thing | fourth thing | fifth thing | sixth thing | seventh thing |
        | a     | b             | c  | d           | e            | f           | g           | h             |
        | i     | j             | k  | l           | m            | n           | o           | p             |

# steps file    
@given('I put "{first_thing}", "{second_thing}", "{third_thing}", "{fourth_thing}", "{fifth_thing}", "{sixth_thing}", "{seventh_thing}",  in a blender')
def step_given_put_thing_into_blender(context, first_thing, second_thing, third_thing, fourth_thing, fifth_thing, sixth_thing, seventh_thing):
    context.blender = Blender()
    context.blender.add(thing)
...

その点は明らかだと思います。それらすべてを明示的に言及することなく、大きなテーブルからステップ定義に例を転送する可能性はありますか? たとえば、それらはテキストで言及されていなくてもコンテキスト変数のどこかに保存されていますか (まだそこには見つかりませんでした)?

4

2 に答える 2

1

実際に大規模なシナリオ アウトライン テーブルを操作する必要があるかどうかを検討することは別として (他の回答を参照)、指定された/いつ/その後のステップですべてのパラメーターを明示的に言及しなくても、現在のテーブル行にアクセスすることは実際に可能ですcontext.active_outline (これは少しです)。ドキュメントの付録に隠されています)

context.active_outline次の方法でアクセスできるbehavior.model.rowオブジェクトを返します。

  • context.active_outline.headings現在反復されている行が何であるかに関係なく、テーブルヘッダーのリストを返します(質問の例ではfirst_thingsecond_thingなど)
  • context.active_outline.cells現在反復されている行のセル値のリストを返します(質問の例ではabcなど)
  • インデックスベースのアクセスcontext.active_outline[0]は、最初の列からセル値を返します (見出しに関係なく) など。
  • 名前ベースのアクセスのようなものは、インデックスに関係なく、 first_thingヘッダーcontext.active_outline['first_thing']を持つ列のセル値を返します

ascontext.active_outline.headingsおよびreturn リストでは、見出しと値のペアなどを反復処理するなどのcontext.active_outline.cells便利なこともできます。for heading, cell in zip(context.active_outline.headings, context.active_outline.cells)

于 2016-08-01T08:36:51.390 に答える