一連の区切りテキストファイルに対して BDD/Gherkin スタイルのテストを作成するために、Python の振る舞いライブラリを使用しようとしています。
典型的なシナリオは次のようになります。
Scenario: Check delivery files for illegal values
Given a file system path to the delivery folder
When I open each file and read its values
Then all values in the "foo" column are smaller than 1
And all values in the "fizz" column are larger than 2
多数のファイルがあり、各ファイルには多数の行が含まれているため、それらすべてをシナリオ アウトラインにハードコードすることはできません。さらに、ファイル全体を一度にメモリに読み込むのは避けたいと思いますが、ジェネレーターを使用して行を 1 つずつ反復処理します。
以下を試しました。ただし、これは、ステップごとに各行が何度も読み取られるため、大規模なデータセットや大量の条件では非常に非効率的then
です。複数のステップ間で単一の行を渡し、次の行then
の最初のステップからやり直す可能性はありますthen
か?
それとも、BDD/Gherkin はこの種のテストには適していませんか? 代替案は何ですか?
import csv
import itertools
import os
@given('a file system path to the delivery folder')
def step(context):
context.path = '/path/to/delivery/files'
@when('I open each file and read its values')
def step(context):
file_list = os.listdir(context.path)
def row_generator():
for path in file_list:
with open(path, 'rb') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
yield row
context.row_generator = row_generator
# itertools.tee forks off the row generator so it can be used multiple times instead of being exhausted after the first 'then' step
@then('all values in the "foo" column are smaller than 1')
def step(context):
for row in itertools.tee(context.row_generator(), 1)[0]:
assert row['foo'] < 1
@then('all values in the "bar" column are larger than 2')
def step(context):
for row in itertools.tee(context.row_generator(), 1)[0]:
assert row['bar'] > 2