[英]How to deal with iteration/looping in Python behave or BDD Scenarios in general?
我正在尝试使用Python的行为库在一堆分隔的文本文件上编写一些BDD /小黄瓜风格的测试。
典型的情况如下所示:
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
由于有很多文件,并且每个文件包含很多行,因此不可能将它们全部硬编码到方案大纲中。 而且,我想避免一次将整个文件读入内存,而是使用生成器逐行遍历行。
我尝试了以下方法。 然而,这是对大数据集和大量的条件,因为每一行反复地读每一个非常低效的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
德克(Dirk),您可以考虑编写脚本以生成方案大纲(考虑到csv文件)。 这样,您可以依赖标准行为/小黄瓜功能。 尽可能简单,因为它会引入另一个可能出错的地方。 但是,它简化了您的步骤文件,因此您可以专注于实际的功能测试,而不是读取csv文件的机制。 根据数据集的大小,您可能还会遇到内存限制,因为我假设表仅保留在内存中。 这不是一个很好的解决方案,而是“替代方案”。
为了实现我的方案的一般循环-我已经完成了以下操作。 当然,您必须编写步骤定义,就像定义一个日志条目一样简单。
@test
Scenario Outline: Repeat Step in Loop
Given we repeat the same test X times
When we do something
Then we analyze results
Examples: Iteration
| X |
| 1 |
| 2 |
| 3 |
欢迎有更好的想法。 迭代不是程序化的,必须进行硬编码(使用小脚本生成该Examples表)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.