44

before(:suite)RSpecおよびMiniTestに代わるものはありafter(:suite)ますか?

カスタムテストランナーが適切であると思われますが、それが一般的な要件ではないとは想像できないため、誰かが実装した可能性があります。:-)

4

8 に答える 8

30

利用可能な方法がsetup()ありteardown()ます。ドキュメントには、利用可能なものもリストbefore()after()れています。

編集:各テストの前、またはスイート全体が終了する前または後に何かを実行しようとしていますか?

于 2011-05-04T13:11:55.613 に答える
24

上記のCaleyの回答とコメントで述べたように、MiniTest::Unitには関数が含まれていますafter_testsbefore_tests同等または同等のものはありませんが、minitest_helper.rbファイル内のコードはテストスイートの前に実行する必要があります。これにより、このような機能が実行されます。

警告:Rubyではまだ比較的新しく、Minitestでは非常に新しいので、間違っている場合は修正してください:-)

于 2012-01-19T10:09:44.940 に答える
21

これを現在のバージョンのMinitest(5.0.6)で機能させるには、を使用する必要がrequire 'minitest'ありますMinitest.after_run { ... }

warn "MiniTest::Unit.after_tests is now Minitest.after_run. ..."

https://github.com/seattlerb/minitest/blob/master/lib/minitest.rb https://github.com/seattlerb/minitest/blob/master/lib/minitest/unit.rb

于 2013-08-24T19:47:31.660 に答える
6

テストの前にコードを実行するには、を使用しますbefore。ここでは、インスタンスのコンテキストで操作しています。おそらく、によって暗黙的に生成されたクラスのコンテキストで操作しているdescribeため、で設定されたインスタンス変数beforeは、各テストでアクセスできます(itブロック内など)。

すべてのテストの前にコードを実行するには、テストをクラス、サブクラスなどでラップするだけMiniTest::Specです。これで、テスト自体の前に、クラスまたはモジュールの作成、クラス変数の設定、クラスメソッドの呼び出しなどを行うことができ、それらすべてがすべてのテストで使用できるようになります。

例:

require "minitest/autorun"

class MySpec < MiniTest::Spec
  class MyClass
  end
  def self.prepare
    puts "once"
    @@prepared = "prepared"
    @@count = 0
  end
  prepare
  before do
    puts "before each test"
    @local_count = (@@count += 1)
  end
  describe "whatever" do
    it "first" do
      p MyClass
      p @@prepared
      p @local_count
    end
    it "second" do
      p MyClass
      p @@prepared
      p @local_count
    end
  end
end

出力の各行が何を証明するかを説明する中括弧内のコメントとともに、出力は次のとおりです。

once [this code, a class method, runs once before all tests]

Run options: --seed 29618 [now the tests are about to run]
# Running tests:

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
1 [the instance variable from the before block is visible in each test]

before each test [the before block runs before each test]
MySpec::MyClass [the class we created earlier is visible in each test]
"prepared" [the class variable we set earlier is visible in each test]
2 [the instance variable from the before block is visible each test]

(この出力は、テストが実行される順序についての保証を意味するものではないことに注意してください。)

もう1つのアプローチは、既存のbeforeラップコードを使用して、クラス変数フラグで1回だけ実行することです。例:

class MySpec < MiniTest::Spec
  @@flag = nil
  before do
    unless @@flag
      # do stuff here that is to be done only once
      @@flag = true
    end
    # do stuff here that is to be done every time
  end
  # ... tests go here
end
于 2012-09-18T21:31:55.397 に答える
3

これを行う簡単な方法の1つは、保護されたクラスメソッドを記述し、それをで呼び出すことbeginです。

Minitest :: Specの例:

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

...取得するため

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
于 2014-09-24T12:38:01.787 に答える
1

クラスの外にコードを配置するだけです。

これは私がバナーを持つために行うことです。

require 'selenium-webdriver'
require 'minitest/test'
require 'minitest/autorun'

class InstanceTest < Minitest::Test

    def setup
    url     = ARGV.first
    @url    = self.validate_instance(url)
        @driver = Selenium::WebDriver.for :firefox
    end
于 2015-06-02T21:11:56.450 に答える
0

minitestの良いところは、その柔軟性です。+before_suite+コールバックでカスタムMiniTestRunnerを使用しています。この例のようなもの-RubyMinitest:スイートレベルまたはクラスレベルのセットアップ?

そして、minitestにカスタムランナーを使用するように指示します

MiniTest::Unit.runner = MiniTestSuite::Unit.new
于 2012-07-24T07:07:45.443 に答える
0

次のようにtest_helper.rb(またはspec_helper.rb)を更新して、テスト後のコールバックを追加することもできます。

# test_helper.rb

class MyTest < Minitest::Unit
  after_tests do
    # ... after test code
  end
end
于 2014-07-16T13:48:02.633 に答える