【发布时间】:2015-07-13 03:41:01
【问题描述】:
我是 Ruby 和 RSpec 的新手,做了一些研究,发现有几种方法可以从在线帖子中进行数据驱动的枚举测试,但它们不像完整的教程那样深入细节。因此,在我再次详细查看那些在线文章之前,我想先在这里问一下。
这是我基于使用 RSpec 的标准简单方法的设置(定义了 describe & it 块,不导入 RSpec 的部分来只做预期)。然后我尝试为其添加数据驱动能力:
require 'rspec'
require 'csv'
describe "test suite name" do
before :all do
#this CSV mapping method found online...
@device_client = CSV.read path
@descriptor = @device_client.shift
@descriptor = @descriptor.map { |key| key.to_sym }
@device_client.map { |client| Hash[ @descriptor.zip(client) ] }
end
@device_client.each do |client|
describe "#{client[:test_scenario]}" do
if "match some CSV field value"
it "should run this kind of test" do
#additional code as needed
expect(some_actual).to eql(some_expected)
end
end
if "match some other CSV field value"
it "should run that kind of test" do
#additional code as needed
expect(some_actual).to eql(some_expected)
end
end
it "some other test common to all CSV rows" do
#stuff
end
end
end
end
我在这里注意到的是,@device_client 是 nil,因为它现在是结构化的(使用“p @device_client”语句调试以转储内容)。为了使其具有价值,我必须将散列包含在它在范围内的 it 块中(我通常将它放在另一个描述块中,但假设我可以跳过额外的描述)。
我如何重组测试以“读取”相同的内容(对测试的读者)并按照我想要的方式运行?如果重组意味着我不能使用标准的 RSpec 格式并且必须以不同的方式要求 RSpec 组件,那很好(网上的帖子似乎没有遵循简单/基本的 RSpec 格式)。
我认为我的代码解释起来相当简单。如果不是,目的是使用 CSV 输入来动态构建测试。每个 CSV 行是一个具有多个测试的场景 - 1 个测试根据 CSV 字段值而有所不同,因此 ifs,其余测试对所有场景都是通用的。我们对文件中的尽可能多的 CSV 场景行重复此设置。而 before all 块是我们处理 CSV 数据之前的全局设置。
在重组中,理想情况下,我希望保留 describe & it 文本描述块(或与其等效的部分),以便在测试结果中显示它们描述测试,而不仅仅是一堆期望。
【问题讨论】: