【问题标题】:How to run a spec with multiple sets of data?如何运行具有多组数据的规范?
【发布时间】:2018-03-15 20:15:53
【问题描述】:

我正在使用 Ruby、RSpec、Watir 等。我是新手。我需要使用不同的数据集运行规范。我曾经在 Selenium 中从 excel 表中读取数据或通过 xml 传递数据。我该怎么做?

【问题讨论】:

    标签: ruby rspec data-driven-tests


    【解决方案1】:

    您仍然可以从 excel 或 xml 中读取数据并将其作为实例变量保存在您的规范文件中。

    使用nokogirigem:

    require 'nokogiri'
    
    RSpec.describe 'Your Feature' do
      context 'Using dataset 1', :dataset1 do
        let :data do
          File.open('dataset1.xml') { |f| Nokogiri::XML(f) }
        end
    
        it 'test with dataset 1' do
          # describe your test here
          puts data  # data returns a Nokogiri::XML::Document object
        end
      end
    
      context 'Using dataset 2', :dataset2 do
        let :data do
          File.open('dataset2.xml') { |f| Nokogiri::XML(f) }
        end
    
        it 'test with dataset 2' do
          # describe your test here
          puts data  # data returns a Nokogiri::XML::Document object
        end
      end
    end
    

    您可以使用CSSXPath 查询解析存储在data 变量中的Nokogiri::XML::Document。阅读更多关于nokogiri api here

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2020-09-03
      • 2021-10-28
      • 2022-06-10
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多