【问题标题】:Ruby/Minitest: How can I run #setup for all tests but one?Ruby/Minitest:我怎样才能为除一个之外的所有测试运行#setup?
【发布时间】:2018-10-26 15:36:18
【问题描述】:

我有一个看起来像这样的测试文件:

class ThingsTest < ActionDispatch::IntegrationTest
  def setup
    ...
  end

  # Test 1
  test 'it should do something' do
    ...
  end

  # Test 2
  test 'it should do something else' do
    ...
  end

  ...

  # Test 50
  test 'it should do another thing' do
    ...
  end
end

对于测试 1 到 49,我希望触发 setup 方法。但是,对于最终测试,我没有。有没有办法阻止#setup 运行最终测试?

【问题讨论】:

    标签: ruby minitest


    【解决方案1】:

    您可以创建另一个类并在那里提取第 50 个测试用例,或者使用自定义设置方法:

    def my_setup
      ...
    end
    
    test 'test1' do
      my_setup
    end
    
    ...
    
    test 'test49' do
      my_setup
    end
    
    test 'test50' do
    # no my_setup here
    end
    

    您还可以切换到rspec,在这里您可以轻松地将测试用例与describes 和context 分开,例如:

    describe 'block1' do
      before do
      #setup here
      end
    
      it 'test1' do
      end
    
      ...
    end
    
    describe 'block2' do
      it 'test50' do
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2017-09-02
      • 2022-11-28
      • 2012-08-23
      • 2023-03-27
      相关资源
      最近更新 更多