before(:suite)
RSpecおよびMiniTestに代わるものはありafter(:suite)
ますか?
カスタムテストランナーが適切であると思われますが、それが一般的な要件ではないとは想像できないため、誰かが実装した可能性があります。:-)
before(:suite)
RSpecおよびMiniTestに代わるものはありafter(:suite)
ますか?
カスタムテストランナーが適切であると思われますが、それが一般的な要件ではないとは想像できないため、誰かが実装した可能性があります。:-)
利用可能な方法がsetup()
ありteardown()
ます。ドキュメントには、利用可能なものもリストbefore()
さafter()
れています。
編集:各テストの前、またはスイート全体が終了する前または後に何かを実行しようとしていますか?
上記のCaleyの回答とコメントで述べたように、MiniTest::Unit
には関数が含まれていますafter_tests
。before_tests
同等または同等のものはありませんが、minitest_helper.rb
ファイル内のコードはテストスイートの前に実行する必要があります。これにより、このような機能が実行されます。
警告:Rubyではまだ比較的新しく、Minitestでは非常に新しいので、間違っている場合は修正してください。:-)
これを現在のバージョンの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
各テストの前にコードを実行するには、を使用します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
これを行う簡単な方法の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
クラスの外にコードを配置するだけです。
これは私がバナーを持つために行うことです。
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
minitestの良いところは、その柔軟性です。+before_suite+コールバックでカスタムMiniTestRunnerを使用しています。この例のようなもの-RubyMinitest:スイートレベルまたはクラスレベルのセットアップ?
そして、minitestにカスタムランナーを使用するように指示します
MiniTest::Unit.runner = MiniTestSuite::Unit.new
次のようにtest_helper.rb(またはspec_helper.rb)を更新して、テスト後のコールバックを追加することもできます。
# test_helper.rb
class MyTest < Minitest::Unit
after_tests do
# ... after test code
end
end