【问题标题】:Run rspec test suite multiple times with different parameters使用不同的参数多次运行 rspec 测试套件
【发布时间】:2020-04-23 01:41:15
【问题描述】:

我有一套规范要运行。我想每次使用不同的参数多次运行规范。例如,我正在针对两个不同的数据库版本测试 SQL 脚本。测试用例相同,但连接字符串不同。我将如何实现这一目标? 我是 RSpec 的新手,我能够让整个套件适用于一个版本。只需要知道如何使用不同的参数重新运行?

我查看了Class:RSpec::Core::Runner,但从文档中我不太清楚如何利用它来运行多次?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    您可以使用env variables 解决此问题。假设您要为两个不同的 MySQL 数据库运行 rspec。你可以像这样定义你的数据库连接:

    db_client = Mysql2::Client.new(database: ENV['DB_NAME'])
    

    现在您可以像这样运行您的 rspec:

    DB_NAME=your_custom_db_name rspec
    DB_NAME=other_db_name rspec
    

    【讨论】:

    • 非常感谢波维拉斯!这解决了我的问题,我希望以 RSpec 的方式学习这一点。因此,对于更复杂的数据驱动套件,我可以应用它。您知道驱动 RSpec 测试的数据方式吗?
    • 没有“RSpec 的做法”。 RSpec 实际上并不了解或关心您的数据库(除了在 r​​spec-rails 中测试挂起迁移的钩子)。大多数 gem 使用 Travis 和 ENV 变量来测试不同的 ruby​​ 版本、数据库等。
    【解决方案2】:

    你可以使用shared_examples来实现你想要的。

    这是一个例子:

    RSpec.describe 'shared_examples' do
      shared_examples 'is palendrome' do |word|
        it 'is equal to itself if reversed' do
          expect(word.reverse).to eq(word)
        end
      end
    
      context 'with the word racecar' do
        # Runs every example is the shared_examples block and passes
        include_examples 'is palendrome', 'racecar'
      end
    
      context 'with the word apple' do
        # Runs every example is the shared_examples block but fails
        include_examples 'is palendrome', 'apple'
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2013-06-13
      相关资源
      最近更新 更多