Before/After Suite when using Ruby MiniTest

前端 未结 8 1052
时光说笑
时光说笑 2020-12-09 07:30

Is there an alternative to RSpec\'s before(:suite) and after(:suite) in MiniTest?

I suspect that a custom test runner is in order, however

8条回答
  •  萌比男神i
    2020-12-09 08:02

    One simple way to do this is to write a guarded class method, and then call that in a begin.

    A Minitest::Spec example:

    describe "my stuff" do
      def self.run_setup_code
        if @before_flag.nil?
          puts "Running the setup code"
          @before_flag = true
        end
      end
    
      before do
        self.class.run_setup_code
      end
    
      it "will only run the setup code once" do
        assert_equal 1, 1
      end
    
      it "really only ran it once" do
        assert_equal 1,1
      end
    end
    

    ...to get

    Run options: --seed 11380
    
    # Running:
    
    Running the setup code
    ..
    
    Finished in 0.001334s, 1499.2504 runs/s, 1499.2504 assertions/s.
    
    2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
    

提交回复
热议问题